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 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短