我需要删除字符串中单词后面的空格。这可以在一行代码中完成吗?
示例:
string = " xyz "
desired result : " xyz"
答案 0 :(得分:137)
答案 1 :(得分:1)
words = " first second "
# remove end spaces
def remove_end_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_end_spaces(words))
print(remove_first_end_spaces(words))
print(remove_all_spaces(words))
我希望这会有所帮助。