Python中刪除空白字元

2022-06-24 09:06:10 字數 1137 閱讀 3393

去除空格

去除左右兩邊的空格使用str.strip():

去除所有空格

使用str.replace():

去除空白符

去除所有的空白符

使用str.splite()及join

使用正則

使用str.translate()

只去除左邊的空白字元

使用str.lstrip()

使用正則

只去除右邊的空白字元

使用str.rstrip()

使用正則

僅去除重複的空白頁

使用正則

綜上,str. strip()會移除字串中開頭與結束(左右兩側)的空白字元,中間部分的空白字元是不會移除的。

strip方法中可自定義要移除的字元,如下面這個示例,移除的為字串中兩側的逗號

Python 字串之刪除空白字元或某字元或字串

strip rstrip lstrip 分別用來刪除兩端 右端 左端 連續的空白字元或字符集 s abc s2 s.strip 刪除空白字元 print s2 abc s3 n nhello world n n strip 刪除空白字元 print s3 hello world s4 aaaassd...

scanf與空白字元

scanf函式在遇到以下情況會停止乙個資料的輸入,是乙個資料哦,不是停止這個函式哦!1 空白字元 空白字元是指空格鍵,tab鍵,回車鍵 2 遇寬度結束 3 遇非法輸入 因此呢,當我們在用scanf輸入乙個字串的時候,空格已出現就停止了,比如 includeint main 執行發現 可我想要的是 h...

html中的空白字元問題

1 當我們想使用百分比來進行兩個盒子的併排 doctype html html lang en head meta charset utf 8 style html,body container container div left right style head body div class c...