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)