मुझे स्ट्रिंग में शब्द के बाद व्हाट्सएप को हटाने की आवश्यकता है। क्या यह कोड की एक पंक्ति में किया जा सकता है?
उदाहरण:
string = " xyz "
desired result : " xyz"
मुझे स्ट्रिंग में शब्द के बाद व्हाट्सएप को हटाने की आवश्यकता है। क्या यह कोड की एक पंक्ति में किया जा सकता है?
उदाहरण:
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))
मुझे आशा है कि यह उपयोगी है।