国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 肥东县| 城市| 南昌市| 贵溪市| 崇阳县| 东乌| 霍城县| 五家渠市| 双城市| 桑日县| 教育| 禄丰县| 衡阳县| 桂阳县| 灵山县| 筠连县| 托克托县| 涟水县| 漾濞| 普陀区| 康马县| 定州市| 古丈县| 昆明市| 淮南市| 新乡县| 佛教| 年辖:市辖区| 泌阳县| 周宁县| 文昌市| 信宜市| 读书| 潮安县| 绥德县| 平昌县| 固安县| 霍州市| 澄城县| 顺昌县| 宜宾县|