當前位置:成語大全網 - 書法字典 - python如何去除文本中多余的空格?

python如何去除文本中多余的空格?

'''

在Python中,有三個函數可以刪除字符串處理函數中的空格:

脫衣?同時刪除左右空格。

lstrip?刪除左邊的空格。

rstrip?刪除右邊的空格。

'''

#具體例子如下:

a= "?gho?stwwl?”

print(a.lstrip())

print(a.rstrip())

print(a.strip())

#去掉中間多余的空格

s= ' '

為了什麽?我?在?範圍(len(a)):

如果?a[i]== '?'?然後呢。我& ltlen(a)-1?然後呢。a[i+1]== '?':

繼續

s+=a[i]

print #與strip()壹起使用,所有多余的空格都被刪除。