python去除字符串中间空格的方法

1、使用字符串函数replace

1 a = 'hello world'
2 a.replace(' ', '')
3 # 'helloworld'

2、使用字符串函数split

1 a = ''.join(a.split())
2 print(a)
3 # helloworld

3、使用正则表达式

1 import re
2 strinfo = re.compile()
3 strinfo = re.compile(' ')
4 
5 b = strinfo.sub('', a)
6 print(b)
7 # helloworld

4、int(对于数字)

content = input("请输入内容:").strip() #可输入1 + 3
# print(''.join(content.split()))
index = content.find("+")
a = content[0:index]
b = content[index+1:]
print(int(a)+int(b))  #转化以后,自动去除空格