문자열에서 단어 뒤의 공백을 제거해야합니다. 한 줄의 코드로이 작업을 수행 할 수 있습니까?
예:
string = " xyz "
desired result : " xyz"
문자열에서 단어 뒤의 공백을 제거해야합니다. 한 줄의 코드로이 작업을 수행 할 수 있습니까?
예:
string = " xyz "
desired result : " xyz"
답변:
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))
도움이 되었기를 바랍니다.