文字列内の単語の後の空白を削除する必要があります。これは1行のコードで実行できますか?
例:
string = " xyz "
desired result : " xyz"
文字列内の単語の後の空白を削除する必要があります。これは1行のコードで実行できますか?
例:
string = " xyz "
desired result : " xyz"
回答:
>>> " xyz ".rstrip()
' xyz'
詳細rstripでのドキュメント
words = " first second "
# remove end spaces
def remove_first_spaces(string):
return "".join(string.rstrip())
# remove first and end spaces
def remove_first_end_spaces(string):
return "".join(string.rstrip().lstrip())
# remove all spaces
def remove_all_spaces(string):
return "".join(string.split())
print(words)
print(remove_first_spaces(words))
print(remove_first_end_spaces(words))
print(remove_all_spaces(words))
これがお役に立てば幸いです。