在使用python语言开发中经常会碰到,需要大写转小写,小写转换大写,甚至,字符串中的单词首字母大写,以及字符串手字字母大写的问题,只需要用下面的几个函数,就可以轻松实现效果; capitalize() 将字符串的第一个字符转换成大写 title() 将字符串内的单词第一个首字母转换成大写 lower() 将字符串中大写转换成小写 upper() 将字符串小写转换成大写
- # 将字符串的第一个字符转换成大写
- mystr = "hello world and itcast and itheima and python"
- print(mystr.capitalize())
-
-
- # title()将字符串内的单词第一个首字母转换成大写
- print(mystr.title())
-
- # lower() 将字符串中大写转换成小写
- print(mystr.lower())
-
- # upper() 将字符串小写转换成大写
- print(mystr.upper())
执行结果:
- Hello world and itcast and itheima and python
- Hello World And Itcast And Itheima And Python
- hello world and itcast and itheima and python
- HELLO WORLD AND ITCAST AND ITHEIMA AND PYTHON