码迷,mamicode.com
首页 > 编程语言 > 详细

python3----转换大小写(upper lower capitalize and title)

时间:2018-01-11 22:21:55      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:false   为什么   首字母   color   result   使用   api   class   错误   

和其他语言一样,Python为string对象提供了转换大小写的方法:upper() 和 lower()。还不止这些,Python还为我们提供了首字母大写,其余小写的capitalize()方法,以及所有单词首字母大写,其余小写的title()方法。函数较简单,看下面的例子:

 1 s = hEllo pYthon
 2 print(s.upper())
 3 print(s.lower())
 4 print(s.capitalize())
 5 print(s.title())
 6 
 7 results:
 8 
 9 HELLO PYTHON
10 hello python
11 Hello python
12 Hello Python

 

判断大小写

Python提供了isupper(),islower(),istitle()方法用来判断字符串的大小写。注意的是:
1. 没有提供 iscapitalize()方法,下面我们会自己实现,至于为什么Python没有为我们实现,就不得而知了。
2. 如果对空字符串使用isupper(),islower(),istitle(),返回的结果都为False。

1 print(A.isupper()) #True
2 print(A.islower()) #False
3 print(Python Is So Good.istitle()) #True
4 #print ‘Dont do that!‘.iscapitalize() #错误,不存在iscapitalize()方法

 

python3----转换大小写(upper lower capitalize and title)

标签:false   为什么   首字母   color   result   使用   api   class   错误   

原文地址:https://www.cnblogs.com/jonm/p/8270324.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!