如何在 Python 中去除字符串中的空格?
为了从字符串中删除空格和尾随空格,Python 提供了 strip([str]) 内置函数。此函数在删除空格(如果存在)后返回字符串的副本。否则返回原始字符串。
string = " javatpoint " string2 = " javatpoint " string3 = " javatpoint" print(string) print(string2) print(string3) print("After stripping all have placed in a sequence:") print(string.strip()) print(string2.strip()) print(string3.strip())
javatpoint javatpoint javatpoint
After stripping all have placed in a sequence:
javatpoint