码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-10-27 17:07:07      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:inpu   code   使用   去除空格   hello   字符串   去除   info   compile   

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))  #转化以后,自动去除空格

 

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

标签:inpu   code   使用   去除空格   hello   字符串   去除   info   compile   

原文地址:https://www.cnblogs.com/shihaoyu/p/9862208.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!