当前位置:首页 » 编程语言 » 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

热点内容
去除头条中的缓存 发布:2025-07-14 01:27:38 浏览:783
php开启错误 发布:2025-07-14 01:16:49 浏览:998
esp数据库 发布:2025-07-14 01:16:44 浏览:980
python查找文件路径 发布:2025-07-14 01:16:03 浏览:514
phpapachetomcat 发布:2025-07-14 01:08:41 浏览:123
服务器运维看什么书 发布:2025-07-14 01:07:32 浏览:988
密码器动态密码怎么弄 发布:2025-07-14 00:44:27 浏览:386
小米怎么把视频加密 发布:2025-07-14 00:42:59 浏览:406
在线申诉找回密码根本什么都没有 发布:2025-07-14 00:41:22 浏览:306
拉新用什么安卓手机可以 发布:2025-07-14 00:41:19 浏览:418