python内置函数 upper lower capitalize title 区别和使用

前端之家收集整理的这篇文章主要介绍了python内置函数 upper lower capitalize title 区别和使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

upper()              字符串中字母由小写变为大写

lower()              字符串中字母由大写变为小写

capitalize()        字符串中字母首字母大写其余小写

title()                 字符串中字母每个单词的首字母大写其余小写

示例:

text_1='hello word'
text_2='HELLO WORD'
text_3='hello WORD'
text_4='hELLO WORD'

print(text_1.upper())
print(text_1.lower())
print(text_1.capitalize())
print(text_1.title())


# 输出结果
HELLO WORD
hello word
Hello word
Hello Word

问题升级
让字符串的首字母变为大写,而字符串其它部分大小写不变如何写?

代码示例:

str='heLLo Word'

new_str='{}{}'.format(str[0].upper(),str[1:])
print(new_str)


猜你在找的Python相关文章