当前位置:首页 » 编程语言 » python去除字符串空格

python去除字符串空格

发布时间: 2022-12-12 12:45:16

python 去除字符串中的空格

将字符串中的空格去除,字符串的长度就减少了,开始计算出的len(str)长度是原始字符串的长度,下标当然会越界

print'pleaseinputastring:'
string=raw_input('>')
string=string.replace('','')
printstring

㈡ python re模块中怎么去掉字符串空格

三种方法如下:
用replace函数:

your_str.replace(' ', '')
a = 'hello word' # 把a字符串里的word替换为python
a.replace('word','python') # 输出的结果是hello python

用split断开再合上:

''.join(your_str.split())

用正则表达式来完成替换:

import re strinfo = re.compile('word')
b = strinfo.sub('python',a)
print b
# 结果:hello python

㈢ python去掉字符串所有空格

字符串,rm为要删除的字符序列

str.strip(rm) : 删除s字符串中开头、结尾处,位于 rm删除序列的字符

str.lstrip(rm) : 删除s字符串中开头(左边)处,位于 rm删除序列的字符

str.rstrip(rm) : 删除s字符串中结尾(右边)处,位于 rm删除序列的字符

str.replace(‘s1’,’s2’) : 把字符串里的s1替换成s2。故可以用replace(’ ‘,”)来去掉字符串里的所有空格

str.split() : 通过指定分隔符对字符串进行切分,切分为列表的形式。

去除两边空格:

>>> str = ' hello world '
>>> str.strip()
'hello world'
1
2
3
1
2
3
去除开头空格:
>>> str.lstrip()
'hello world '
1
2
1
2
去除结尾空格:
>>> str.rstrip()
' hello world'
1
2
1
2
去除全部空格:
>>> str.replace(' ','')
'helloworld'
1
2
1
2
将字符串以空格分开:
>>> str.split()
['hello', 'world']
>>>

㈣ python怎么取消字符串之间的空格

name = "i am sophie"
name = name.replace(" ","")
print(name)
如果只是单纯去掉左右两边的,比如“ 你好”,可以考虑strip(lstrip、rstrip、strip)

㈤ python字符串结果如何消除空格

print('Index='+str(s.rfind(c)))

㈥ 怎么用python删除CSV中字符串多余的空格

这个处理可以用excel打开,直接用函数trim就可以去掉了。

没必要这么处理
=TRIM("XXXXXX ")

㈦ 已知变量string='Python是一种解释型语言',怎么将字符串string两侧空格去除

用法是先strip()后upper()就可以把前后的空格去掉并且把python转为PYTHON

热点内容
java返回this 发布:2025-10-20 08:28:16 浏览:593
制作脚本网站 发布:2025-10-20 08:17:34 浏览:888
python中的init方法 发布:2025-10-20 08:17:33 浏览:581
图案密码什么意思 发布:2025-10-20 08:16:56 浏览:765
怎么清理微信视频缓存 发布:2025-10-20 08:12:37 浏览:684
c语言编译器怎么看执行过程 发布:2025-10-20 08:00:32 浏览:1012
邮箱如何填写发信服务器 发布:2025-10-20 07:45:27 浏览:255
shell脚本入门案例 发布:2025-10-20 07:44:45 浏览:113
怎么上传照片浏览上传 发布:2025-10-20 07:44:03 浏览:806
python股票数据获取 发布:2025-10-20 07:39:44 浏览:712