新聞中心
python中刪除字符串中空格的方法:

專注于為中小企業(yè)提供成都做網(wǎng)站、成都網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)定邊免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了1000多家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
1、使用replace()函數(shù),把所有的空格(" ")替換為("")
def remove(string):
return string.replace(" ", "");
string = ' H E L L O ! ';
print("原字符串:"+string) ;
print("\n新字符串:"+remove(string)) ;輸出結(jié)果:
原字符串: H E L L O !
新字符串:HELLO!
2、使用lstrip()函數(shù)刪除字符串左邊空格
lstrip()方法語法:
str.lstrip([chars])
示例:
#!/usr/bin/python
str = " this is string example....wow!!! ";
print str.lstrip();
str = "88888888this is string example....wow!!!8888888";
print str.lstrip('8');輸出結(jié)果:
this is string example....wow!!!
this is string example....wow!!!8888888
3、使用rstrip()函數(shù)刪除字符串末尾空格
rstrip()方法語法:
str.rstrip([chars])
示例:
#!/usr/bin/python
str = " this is string example....wow!!! ";
print str.rstrip();
str = "88888888this is string example....wow!!!8888888";
print str.rstrip('8');輸出結(jié)果如下:
this is string example....wow!!!
88888888this is string example....wow!!!
新聞名稱:創(chuàng)新互聯(lián)Python教程:python中如何刪除字符串中的空格
文章起源:http://m.5511xx.com/article/cocddho.html


咨詢
建站咨詢
