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

首頁 > 編程 > Python > 正文

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

2020-02-23 01:19:31
字體:
供稿:網(wǎng)友

目的

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

方法

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

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! |

其中提供的參數(shù)chars用來刪除特定的符號(hào),注意空格并沒有被移除,例如:
代碼如下:
In [12]: x='yxyxyxxxyy Hello xyxyxyy'

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

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 栖霞市| 瓦房店市| 玉田县| 临桂县| 岢岚县| 小金县| 金寨县| 高尔夫| 平和县| 防城港市| 龙山县| 怀来县| 行唐县| 宁武县| 白水县| 肃北| 邢台县| 高清| 年辖:市辖区| 昂仁县| 左云县| 丹寨县| 芒康县| 将乐县| 珠海市| 西畴县| 临泉县| 曲松县| 巍山| 德格县| 花莲市| 邵武市| 从江县| 临颍县| 鄄城县| 通海县| 黔东| 讷河市| 郎溪县| 白河县| 塔河县|