我需要转换字符串:
"There are 6 spaces in this string."
至:
"Thereare6spacesinthisstring."
你可以使用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删除结尾/开头(或两者!)的白色字符。