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