Python中的字符串大写()
在本教程中,我们将学习有关字符串的大写方法的知识。让我们开始学习本教程。
大写方法将字符串的第一个字母大写。如果首字母为非字母或大写字母,则不会产生任何影响。
请按照以下步骤编写程序。
初始化字符串。
使用大写方法将字符串的第一个字母转换为大写。
将结果存储在新变量中。
打印结果。
示例
# initializing the string string = "nhooo" # capitalizing the first letter result = string.capitalize() # printing the result print(result)
输出结果
如果运行上面的代码,则将得到以下结果。
Nhooo
现在,将前面示例中字符串的第一个字母更改为大写。并再次运行。
示例
# initializing the string string = "Nhooo" # capitalizing the first letter result = string.capitalize() # printing the result print(result)
输出结果
如果运行上面的代码,则将得到以下结果。
Nhooo
在上一个示例中,将非字母添加为字符串的第一个字母。并运行它。
示例
# initializing the string string = "nhooo" # capitalizing the first letter result = string.capitalize() # printing the result print(result) nhooo
输出结果
如果运行上面的代码,则将得到以下结果。
nhooo
如果字符串中有多个单词和多行会发生什么?大写方法只会将字符串的第一个字母大写。
运行以下代码并查看。
示例
# initializing the string string = "nhooo is great website to learn new things. and it's free" # capitalizing the first letter result = string.capitalize() # printing the result print(result)
输出结果
如果运行上面的代码,则将得到以下结果。
Nhooo is great website to learn new things. and it's free
结论
如果您对本教程有任何疑问,请在评论部分中提及。
热门推荐
10 哥哥结婚的祝福语简短
11 冬季祝福语大全 简短的
12 麻辣拌店祝福语简短
13 送离职同事简短祝福语
14 朋友结婚致辞祝福语简短
15 五二零朋友祝福语大全简短
16 讲课祝福语简短10字
17 诗意新婚祝福语高级简短
18 老婆生日祝福语短句简短