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

熱點內容
手機優酷緩存視頻格式 發布:2025-05-15 04:13:45 瀏覽:208
公益電影分鏡頭腳本插畫 發布:2025-05-15 04:08:37 瀏覽:959
數據壓縮編碼 發布:2025-05-15 03:58:44 瀏覽:725
java字元為空 發布:2025-05-15 03:57:11 瀏覽:546
速訊安卓哪裡下載 發布:2025-05-15 03:55:02 瀏覽:48
緩存區數據讀寫原理 發布:2025-05-15 03:39:57 瀏覽:585
編譯器生成的是二進制文件嗎 發布:2025-05-15 03:38:42 瀏覽:955
運營為什麼區分ios和安卓 發布:2025-05-15 03:30:02 瀏覽:630
主播網站源碼 發布:2025-05-15 02:50:56 瀏覽:168
中文編程語言有哪些 發布:2025-05-15 02:48:59 瀏覽:536