當前位置:首頁 » 編程語言 » 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

熱點內容
安卓jj比賽哪個版本有全記牌器 發布:2025-07-14 08:28:27 瀏覽:72
python腳本執行hive 發布:2025-07-14 08:26:24 瀏覽:488
媒體存儲可以刪除嗎 發布:2025-07-14 08:26:13 瀏覽:812
ios網易雲音樂上傳 發布:2025-07-14 08:16:58 瀏覽:392
榮耀8x可以用方舟編譯器嗎 發布:2025-07-14 08:09:49 瀏覽:277
雲伺服器商家怎麼購買 發布:2025-07-14 08:05:34 瀏覽:45
c語言convert函數 發布:2025-07-14 07:56:34 瀏覽:484
sql存儲過程列印 發布:2025-07-14 07:56:26 瀏覽:658
弄伺服器需要什麼語言 發布:2025-07-14 07:54:27 瀏覽:507
金土火的三才配置是什麼意思 發布:2025-07-14 07:51:21 瀏覽:774