Python strip()方法

描述

Python strip() 方法用于移除字符串头尾指定的字符(默认为空格)。

语法

strip()方法语法:

str.strip([chars]);

参数

  • chars -- 移除字符串头尾指定的字符。

返回值

返回移除字符串头尾指定的字符生成的新字符串。

实例

以下实例展示了strip()函数的使用方法:

#!/usr/bin/python

str = "0000000this is string example....wow!!!0000000";
print str.strip( ‘0‘ );

以上实例输出结果如下:

this is string example....wow!!!
时间: 2024-08-03 19:22:14

Python strip()方法的相关文章

Python strip()方法介绍

描述 Python strip() 方法用于移除字符串头尾指定的字符(默认为空格). 语法 strip()方法语法: str.strip([chars]); 参数 chars -- 移除字符串头尾指定的字符. 返回值 返回移除字符串头尾指定的字符生成的新字符串. 特别注意:移除的字符串的头尾指定字符,不如不是头尾字符不会移除 例1: >>> a = '     123    '>>> a.strip()'123' 例2: >>> a = '  123

python中strip()方法学习笔记

Python strip() 方法用于移除字符串头尾指定的字符(默认为空格). 当使用strip('xxx'),只要字符串头尾有"xxx"中的一个,就会去掉,而不是符合字符串''xxx''才去掉 1 >>> string = 'aaabbbccc' 2 >>> string.strip('abc') 3 '' 4 >>> string2 = 'aaaffbbcc' 5 >>> string2.strip('abc'

strip()方法

描述 Python strip() 方法用于移除字符串头尾指定的字符(默认为空格)或字符序列. 注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符. 语法 strip()方法语法: str.strip([chars]); 参数 chars -- 移除字符串头尾指定的字符序列. 返回值 返回移除字符串头尾指定的字符序列生成的新字符串. 实例 以下实例展示了 strip() 函数的使用方法: 实例(Python 3.0+) #!/usr/bin/python3 str = "*****t

Python中strip方法的妙用

[开胃小菜] 当提到python中strip方法,想必凡接触过python的同行都知道它主要用来切除空格.有以下两种方法来实现. 方法一:用内置函数 #<python> if __name__ == '__main__': str = ' Hello world ' print '[%s]' %str.strip() #</python> 方法二:调用string模块中方法 #<python> import string if __name__ == '__main__

python中的strip()方法

python中字符串str的strip()方法 str.strip()就是把字符串(str)的头和尾的空格,以及位于头尾的\n \t之类给删掉. 例1: str=" python " print(str.strip()) ---> python 例2: str2 = "\n AAAA" print(str2) print(str2.strip()) ---> AAAA AAAA 例3: a= "\n ABC ABC ABC =========&

python练习题:利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法

方法一: # -*- coding: utf-8 -*- # 利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法: def trim(s): while s[:1] == ' ': s = s[1:] while s[-1:] == ' ': s = s[0:-1] return s # 测试: if trim('hello ') != 'hello': print('测试失败!') elif trim(' hello') != 'hello':

Python 字符串方法详解

Python 字符串方法详解 本文最初发表于赖勇浩(恋花蝶)的博客(http://blog.csdn.net/lanphaday),如蒙转载,敬请保留全文完整,切勿去除本声明和作者信息. 在编程中,几乎90% 以上的代码都是关于整数或字符串操作,所以与整数一样,Python 的字符串实现也使用了许多拿优化技术,使得字符串的性能达到极致.与 C++ 标准库(STL)中的 std::string 不同,python 字符串集合了许多字符串相关的算法,以方法成员的方式提供接口,使用起来非常方便. 字符

Python str方法总结

1.返回第一个字母大写 S.capitalize(...) S.capitalize() -> string >>>a = 'shaw' >>> b = a.capitalize() >>> print b Shaw 2.按指定长度填充特定字符 center(...) S.center(width[, fillchar]) -> string >>> a = 'linux' >>> print a.cen

python 字符串方法积累

字符串方法 str.strip([chars]) 返回字符串的副本,删除前导符和尾随符,chars参数为需要被删除的字符集,默认情况下删除空格,chars参数不是前缀或后缀,而是所有chars参数中的组合都被删除 例如: >>> 'www.example.com'.strip('cmowz.') 'example' >>> '   spacious   '.strip() 'spacious' str.join(iterable) 通过iterable将字符串串联起来并