![135a14b574730bee3af4ec060395bacf.png](https://img-blog.csdnimg.cn/img_convert/135a14b574730bee3af4ec060395bacf.png)
大小写转换
转换大小写 和其他语言一样,Python为string对象提供了转换大小写的方法:upper() 和 lower()。还不止这些,Python还为我们提供了首字母大写,其余小写的capitalize()方法,以及所有单词首字母大写,其余小写的title()方法。函数较简单,看下面的例子:
str1 = 'lEARN pYthon wITH BYHY'
print(str1.upper())
print(str1.lower())
print(str1.capitalize())
print(str1.title())
输出结果:
LEARN PYTHON WITH BYHY
learn python with byhy
Learn python with byhy
Learn Python With Byhy
判断大小写
Python提供了isupper(),islower(),istitle()方法用来判断字符串的大小写。注意的是:
- 没有提供 iscapitalize()方法,下面我们会自己实现,至于为什么Python没有为我们实现,就不得而知了。
- 如果对空字符串使用isupper(),islower(),istitle(),返回的结果都为False。
print('BYHY'.isupper()) # True
print('BYHY'.islower()) # False
print('Learn Python With Byhy'.istitle()) # True
# print('Learn Python With Byhy'.iscapitalize()) # 错误,不存在iscapitalize()方法
可以通过白月黑羽在线教程学习Python,让你在学习的路上不孤独!