Python进阶(二十七)-字符串大小写转换
【摘要】 #Python进阶(二十七)-字符串大小写转换 ##大小写转换 和其他语言一样,Python为string对象提供了转换大小写的方法:upper() 和 lower()。还不止这些,Python还为我们提供了首字母大写,其余小写的capitalize()方法,以及所有单词首字母大写,其余小写的title()方法。函数较简单,看下面的例子:
s = "Hello P...
#Python进阶(二十七)-字符串大小写转换
##大小写转换
和其他语言一样,Python为string对象提供了转换大小写的方法:upper() 和 lower()。还不止这些,Python还为我们提供了首字母大写,其余小写的capitalize()方法,以及所有单词首字母大写,其余小写的title()方法。函数较简单,看下面的例子:
s = "Hello PythoN"
#全部大写
print(s.upper())
#全部小写
print(s.lower())
#首字母大写,其余小写
print(s.title())
#每个单次首字母大写
print(s.capitalize())
输出结果:
##判断大小写
Python提供了isupper(),islower(),istitle()方法用来判断字符串的大小写。注意的是:
- 1.没有提供 iscapitalize()方法,至于为什么Python没有为我们实现,就不得而知了。
- 2.如果对空字符串使用isupper(),islower(),istitle(),返回的结果都为False。
print('A'.isupper())
print('S'.islower())
print("Hello Python".istitle())
输出结果:
 
文章来源: shq5785.blog.csdn.net,作者:No Silver Bullet,版权归原作者所有,如需转载,请联系作者。
原文链接:shq5785.blog.csdn.net/article/details/69390312
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)