在使用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

Logo

助力广东及东莞地区开发者,代码托管、在线学习与竞赛、技术交流与分享、资源共享、职业发展,成为松山湖开发者首选的工作与学习平台

更多推荐