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
结论
如果您对本教程有任何疑问,请在评论部分中提及。
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短