国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python去除字符串兩端空格的方法

2019-11-25 17:25:08
字體:
來源:轉載
供稿:網友

目的

  獲得一個首尾不含多余空格的字符串

方法

可以使用字符串的以下方法處理:

string.lstrip(s[, chars])
Return a copy of the string with leading characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the beginning of the string this method is called on.

string.rstrip(s[, chars])
Return a copy of the string with trailing characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the end of the string this method is called on.

string.strip(s[, chars])
Return a copy of the string with leading and trailing characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the both ends of the string this method is called on.

 

具體的效果如下:

復制代碼 代碼如下:

In [10]: x='     Hi,Jack!        '

In [11]: print '|',x.lstrip(),'|',x.rstrip(),'|',x.strip(),'|'
| Hi,Jack!         |      Hi,Jack! | Hi,Jack! |

其中提供的參數chars用來刪除特定的符號,注意空格并沒有被移除,例如:

復制代碼 代碼如下:

In [12]: x='yxyxyxxxyy Hello xyxyxyy'

In [13]: print x.strip('xy')
 Hello

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 雅安市| 郓城县| 山阴县| 武陟县| 汤阴县| 托里县| 亚东县| 铅山县| 平舆县| 广南县| 政和县| 澎湖县| 宜良县| 青铜峡市| 长寿区| 乌拉特后旗| 平顶山市| 沐川县| 广元市| 浪卡子县| 盱眙县| 九寨沟县| 利辛县| 平遥县| 遂川县| 墨脱县| 治多县| 鄂托克前旗| 桂阳县| 建水县| 德钦县| 富民县| 筠连县| 娄底市| 瑞安市| 理塘县| 衡南县| 平阳县| 天水市| 讷河市| 阜南县|