python字符串title(),upper(),lower(),capitalize(),swapcase()

来源:互联网 发布:白手套 知乎 编辑:程序博客网 时间:2024/06/07 20:41

title()

title()方法返回所有单词的第一个字符大写的字符串的一个副本。
语法:

str.title();

返回值
此方法返回其中所有单词的前几个字符都是大写的字符串的一个副本

示例

>>> str="this is a string exemple...wow-bravo">>> print str.title()This Is A String Exemple...Wow-Bravo

istitle()

istitle() 方法检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写。
语法:

str.istitle();

返回值:

如果字符串中所有的单词拼写首字母是否为大写,且其他字母为小写则返回 True,否则返回 False.

示例:

>>>str="this is a string exemple...wow-bravo">>>> print str.istitle()False>>> str='This Is A String Exemple...Wow-Bravo'>>> print str.istitle()True

另:

>>> str="this is a string exemple...wow-bravo">>> print str.title()This Is A String Exemple...Wow-Bravo>>> print str.istitle()False>>> print strthis is a string exemple...wow-bravo

可见,str.title()只是返回了一个副本,而为对原字符串做更改。

upper()。

Python upper() 方法将字符串中的小写字母转为大写字母。
语法:

str.upper();

返回值:
返回小写字母转为大写字母的字符串。

示例:

>>> str="this is a string exemple...wow-bravo">>> print str.upper()THIS IS A STRING EXEMPLE...WOW-BRAVO>>> print strthis is a string exemple...wow-bravo

isupper()。

Python isupper() 方法检测字符串中所有的字母是否都为大写
语法:

str.isupper();

示例:

>>> str = "THIS IS STRING EXAMPLE....WOW!!!">>> print str.isupper()True>>> str = "THIS is string example....wow!!!">>> print str.isupper()False

lower()

Python lower() 方法转换字符串中所有大写字符为小写。
语法:

str.lower();

示例:

>>> str = "THIS IS STRING EXAMPLE....WOW!!!">>> print str.lower()this is string example....wow!!!>>> print strTHIS IS STRING EXAMPLE....WOW!!!

islower()

Python islower() 方法检测字符串是否由小写字母组成。
语法

str.islower();

示例:

>>> str = "THIS is string example....wow!!!">>> print str.islower()False>>> str = "this is string example....wow!!!">>> print str.islower()True

capitalize()

Python capitalize()将字符串的第一个字母变成大写,其他字母变小写。
语法:

str.capitalize();

返回值:
该方法返回一个首字母大写的字符串。

示例:

>>> str = "this is string example....wow!!!">>> print str.capitalize()This is string example....wow!!!>>> print strthis is string example....wow!!!

swapcase()

Python swapcase() 方法用于对字符串的大小写字母进行转换。
语法:

str.swapcase();

示例:

>>> str = "THIS IS STRING EXAMPLE....WOW!!!">>> print str.swapcase()this is string example....wow!!!>>> str = "THIS IS string example....wow!!!">>> print str.swapcase()this is STRING EXAMPLE....WOW!!!>>> print strTHIS IS string example....wow!!!
2 0
原创粉丝点击