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

热点内容
android文件夹重命名 发布:2025-05-15 01:13:50 浏览:481
cns脚本 发布:2025-05-15 01:13:38 浏览:722
数据结构与算法笔试题 发布:2025-05-15 01:04:20 浏览:417
搜狗输入法如何直接编辑配置文件 发布:2025-05-15 00:51:47 浏览:668
电箱都有哪些配置 发布:2025-05-15 00:30:21 浏览:74
安卓qq邀请码在哪里寻找 发布:2025-05-15 00:02:04 浏览:35
三菱fx编程口 发布:2025-05-15 00:01:23 浏览:810
医院招商引资宣传片脚本 发布:2025-05-15 00:01:21 浏览:368
linuxcftp服务器 发布:2025-05-14 23:58:18 浏览:718
探岳什么配置才有驾驶模式选择 发布:2025-05-14 23:53:17 浏览:146