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

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:593
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:888
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:581
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:765
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:684
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:1012
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:255
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:113
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:806
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:712