python字符串方法总结

简介:

    最近想着总结一下python里面关于字符串方法使用的相关知识,无意间发现一个比较好的内容,所以搬移到此,以便后期查找,也分享给大家,如若原文作者有疑问请联系我及时删除,谢谢!

转载于:https://my.oschina.net/junwuwei/blog/29626

判断 – 通常返回一个bool值
str.isalpha() 是否只包含文字
str.isdecimal() 是否只包含数字(多语言数字)
str.isdigit() 是否只包含数字(0~9)
str.isnumeric() 是否只包含数字字符
str.isalnum() 是否只包含文字和数字
str.isidentifier() 是否是合法标识符
str.islower() 是否是小写
str.isupper() 是否全是大写
str.istitle() 是否每词首字母大写
str.isprintable() 是否只包含可打印字符
str.isspace() 是否只包含空白字符
str.startswith(prefix[, start[, end]]) 是否以prefix开头
str.endswith(suffix[, start[, end]]) 是否以suffix结尾
修饰 – 通常返回一个修饰后的字符串
str.capitalize() 返回一个首字母大写的字符串
str.title() 返回每个词首字母大写的字符串
str.expandtabs([tabsize]) "\t"转换成空格
str.upper() 全转换成大写
str.lower() 全转换成小写
str.ljust(width[, fillchar]) 左对齐,右填充
str.rjust(width[, fillchar]) 右对齐,左填充
str.center(width[, fillchar]) 居中,两边填充
str.lstrip([chars]) 去除左空白或自定字符
str.rstrip([chars]) 去除右空白或自定字符
str.strip([chars]) 去除两边空白或自定字符
str.swapcase() 大小写互转
str.zfill(width) 左侧填充0到指定宽,一般用来修饰数字
查找&&替换
str.count(sub[, start[, end]]) 计算[start, end)间,sub出现次数
str.find(sub[, start[, end]])
str.index(sub[, start[, end]])
str.rfind(sub[, start[, end]])
str.rindex(sub[, start[, end]])
str.replace(old, new[, count])
拆分&&组合
str.join(iterable)
str.partition(sep)
str.rpartition(sep)
str.split([sep[, maxsplit]])
str.rsplit([sep[, maxsplit]])
str.splitlines([keepends])
转换
hex(x)
int([number | string[, base]])
len(s)
list([iterable])
oct(x)
ord(c)
repr(object)
reversed(seq)
str([object[, encoding[, errors]]])

↑TOP↑str.isalpha() – 是否只包含文字

代码 结果
print( "中国abc".isalpha() ) True
print( " ".isalpha() ) False
print( "123".isalpha() ) False
print( "".isalpha() ) False

↑TOP↑str.isdecimal() – 是否只包含十进制数字,包括多语言数字

代码 结果
print( "1234567890".isdecimal() ) True
print( "\u0660".isdecimal() ) True
print( "abc".isdecimal() ) False
print( "".isdecimal() ) False

关于其他语言的数字参见 http://www.fileformat.info/info/unicode/category/Nd/list.htm

↑TOP↑str.isdigit() – 是否只包含数字(0~9)

代码 结果
print( "1234567890".isdigit() ) True
print( "\u0660".isdigit() ) True
print( "abc".isdigit() ) False
print( "".isdigit() ) False

↑TOP↑str.isnumeric() – 是否只包含数字字符

代码 结果
print( "1234567890".isnumeric() ) True
print( "\u2155".isnumeric() ) True
print( "abc".isnumeric() ) False
print( "".isnumeric() ) False

关于数字字符参见 http://www.fileformat.info/info/unicode/category/No/list.htm

↑TOP↑str.isalnum() – 是否只包含文字和数字

代码 结果
print( "中国abc123456\u2155".isalnum() ) True
print( " ".isalnum() ) False
print( "\t".isalnum() ) False
print( "".isalnum() ) False

↑TOP↑str.isidentifier() – 是否是合法标识符

代码 结果
print( "if".isidentifier() ) True
print( "中国".isidentifier() ) True
print( "123".isidentifier() ) False
print( "".isidentifier() ) False

↑TOP↑str.islower() – 是否是小写

代码 结果
print( "abc".islower() ) True
print( "aBc".islower() ) False
print( "中国".islower() ) False
print( "".islower() ) False

↑TOP↑str.isupper() – 是否全是大写

代码 结果
print( "HELLO WORLD".istitle() ) True
print( "Hello World".istitle() ) False
print( "世界你好".istitle() ) False
print( "".istitle() ) False

↑TOP↑str.istitle() – 是否每词首字母大写

代码 结果
print( "Hello World".istitle() ) True
print( "Hello world".istitle() ) False
print( "hello world".istitle() ) False
print( "".istitle() ) False

↑TOP↑str.isprintable() – 是否只包含可打印字符

代码 结果
print( "a b".isprintable() ) True
print( "".isprintable() ) True
print( "abc\t".isprintable() ) False
print( "abc\n".isprintable() ) False

↑TOP↑str.isspace() – 是否只包含空白字符

代码 结果
print( " ".isspace() ) True
print( "\t\n".isspace() ) True
print( "a b".isspace() ) False
print( "".isspace() ) False

↑TOP↑str.startswith(prefix[, start[, end]]) – 是否以prefix开头

代码 结果
print( "中国人".startswith("中") ) True
print( "中国人".startswith(("中国","我")) ) True

↑TOP↑str.endswith(suffix[, start[, end]]) – 是否以suffix结尾

代码 结果
print( "中国人".endswith("人") ) True
print( "中国人".endswith(("国人","我")) ) True

↑TOP↑str.capitalize() – 返回一个首字母大写的字符串

代码 print( "the first sentence. the second sentence.".capitalize() )
结果 The first sentence. the second sentence.

↑TOP↑str.title() – 返回每个词首字母大写的字符串

代码 print( "this is a title".title() )
结果 This Is A Title

↑TOP↑str.expandtabs([tabsize]) – "\t"转换成空格

代码 "\t".expandtabs(8)
结果 '        '

↑TOP↑str.upper() – 全转换成大写

代码 print( "abc".upper() )
结果 ABC

↑TOP↑str.lower() – 全转换成小写

代码 print( "ABC".upper() )
结果 abc

↑TOP↑str.ljust(width[, fillchar]) – 左对齐,右填充

代码 print( "我".ljust(4,"们") )
结果

我们们们

↑TOP↑str.rjust(width[, fillchar]) – 右对齐,左填充

代码 print( "我".rjust(4,"=") )
结果

===我

↑TOP↑str.center(width[, fillchar]) – 居中,两边填充

代码 print( "我是分割线".center(30, "=") )
结果 ============我是分割线=============

↑TOP↑str.lstrip([chars]) – 去除左空白或自定字符

代码 '   spacious   '.lstrip()
结果 'spacious   '
代码 'www.example.com'.lstrip('cmowz.')
结果 'example.com'

↑TOP↑str.rstrip([chars]) – 去除右空白或自定字符

代码 '   spacious   '.rstrip()
结果 '   spacious'
代码 'mississippi'.rstrip('ipz')
结果 'mississ'

↑TOP↑str.strip([chars]) – 去除两边空白或自定字符

代码 '   spacious   '.strip()
结果 'spacious'
代码 'www.example.com'.strip('cmowz.')
结果 'example'

↑TOP↑str.swapcase() – 大小写互转

代码 print( "Abc".swapcase() )
结果 aBC

↑TOP↑str.zfill(width) – 左侧填充0到指定宽,一般用来修饰数字

代码 print( "15".zfill(8) )
结果 00000015
代码 print( "-15".zfill(8) )
结果 -0000015

↑TOP↑str.count(sub[, start[, end]]) – 计算[start, end)间,sub出现次数

代码 print( "abababab" .count("abab" )
结果 2



本文转自 tianya1993 51CTO博客,原文链接:http://blog.51cto.com/dreamlinux/1910520,如需转载请自行联系原作者
相关文章
|
11天前
|
Python
python魔法方法如何应用
【4月更文挑战第12天】这个Python示例展示了类继承和方法重写。`Student`类继承自`Person`,并覆盖了`say_hello`方法。通过`super().__init__(name)`调用父类的`__init__`初始化`name`属性,`Student`添加了`age`属性,并在重写的`say_hello`中使用。创建`Student`实例`student`并调用其`say_hello`,输出定制的问候信息。
19 1
|
5天前
|
存储 关系型数据库 MySQL
Python搭建代理IP池实现存储IP的方法
Python搭建代理IP池实现存储IP的方法
|
5天前
|
Python
Python动态IP代理防止被封的方法
Python动态IP代理防止被封的方法
|
5天前
|
数据采集 存储 安全
python检测代理ip是否可用的方法
python检测代理ip是否可用的方法
|
6天前
|
数据可视化 测试技术 Python
在Python和R中使用交叉验证方法提高模型性能
在Python和R中使用交叉验证方法提高模型性能
|
7天前
|
存储 监控 开发工具
对象存储OSS产品常见问题之python sdk中的append_object方法支持追加上传xls文件如何解决
对象存储OSS是基于互联网的数据存储服务模式,让用户可以安全、可靠地存储大量非结构化数据,如图片、音频、视频、文档等任意类型文件,并通过简单的基于HTTP/HTTPS协议的RESTful API接口进行访问和管理。本帖梳理了用户在实际使用中可能遇到的各种常见问题,涵盖了基础操作、性能优化、安全设置、费用管理、数据备份与恢复、跨区域同步、API接口调用等多个方面。
37 9
|
7天前
|
Python
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(上)
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(上)
45 0
|
7天前
|
Python
python学习-函数模块,数据结构,字符串和列表(下)
python学习-函数模块,数据结构,字符串和列表
44 0
|
8天前
|
数据采集 Python
python学习9-字符串
python学习9-字符串
|
12天前
|
机器学习/深度学习 人工智能 算法