python strip,方法使用

描述

python strip() ,用于去除述字符串头尾指定字符(默认为空格或换行符)或字符序列。

注意:此方法只能去除头尾的空格或是换行符,不能去除中间的。

语法:

str.strip([chars])

参数:

chars -- 移除字符串头尾的指定字符序列

返回值:

返回字符串中新生成的序列

实例

 1 # !/usr/bin/python
 2 # -*- coding: UTF-8 -*-
 3 
 4 str = "00000003210python01230000000";
 5 print(str.strip('0'))  # 去除首尾字符 0
 6 
 7 str2 = "   python      "  # 去除首尾空格
 8 print(str2.strip())
 9 
10 str3 = "123python321" # 去除12
11 print(str3.strip('12'))

打印结果:

1 3210python0123
2 python
3 3python3