python去除字符串空格
㈠ 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