Python字符串字母大小写变换

说明:

字符串就是一系列字符,在Python中用引号括起来的都是字符串,引号可以是单引号,也可以是双引号,比如:“This is a book.”  ‘This is an apple.’

接下来简单说明下关于字符串大小写的变换。

 

操作:

casefold() 所有字母小写
lower() 所有字母小写
upper() 所有字母大写
capitalize() 所有字母小写
title() 所有首字母大写,其余字母小写
swapcase() 大小写字母替换
这几个方法都是生成新的字符串,不对原字符串内容进行修改。
demo = 'tHis iS a GOod boOK.'
print(demo.casefold())
print(demo.lower())
print(demo.upper())
print(demo.capitalize())
print(demo.title())
print(demo.swapcase())

  

输出:

this is a good book.
this is a good book.
THIS IS A GOOD BOOK.
This is a good book.
This Is A Good Book.
ThIS Is A goOD BOok.

  

posted @ 2020-02-26 18:55  叛逆期阿飞  阅读(3308)  评论(0编辑  收藏  举报