【发布时间】:2010-11-03 02:23:38
【问题描述】:
我需要转换字符串:
"There are 6 spaces in this string."
到:
"Thereare6spacesinthisstring."
【问题讨论】:
标签: python string python-3.x
我需要转换字符串:
"There are 6 spaces in this string."
到:
"Thereare6spacesinthisstring."
【问题讨论】:
标签: python string python-3.x
您可以使用replace
string = "There are 6 spaces in this string"
string = string.replace(' ', '')
【讨论】:
new = "There are 6 spaces in this old_string.".replace(' ', '')
如果你想去掉所有空格,包括制表符:
new = ''.join( old_string.split() )
【讨论】:
你可以使用 replace() :
print(string.replace(" ", ""))
您还可以使用 rstrip、lstrip 或 strip 删除结尾/开头(或两者!)的白色字符。
【讨论】: