Python 字符串大写转换

位置:首页>文章>详情   分类: Python教程 > 编程技术   阅读(314)   2023-06-26 07:54:28

在 Python 中,capitalize() 方法将字符串大写,即大写给定字符串中的第一个字母,并将所有其他字符(如果有)小写。

除了改变大小写(大写/小写),capitalize() 不会修改原始字符串的内容。

1. capitalize() 语法

capitalize() 的语法非常简单。

string.capitalize()

2. capitalize() 例子

让我们看几个例子来理解 capitalize() 在 Python 中的用法。

示例 1:当第一个字符是字母时大写

string = "god is Great"

capitalizedString = string.capitalize()

print('Old String: ', string)
print('Capitalized String:', capitalizedString)

程序输出。

Old String:  god is Great
Capitalized String: God is great

示例 2:首字符为非字母时大写

请注意大写字母“N”是如何转换为小写字母“n”的。它得出的结论是,即使第一个字符不是字母,capitalize() 方法仍会检查整个字符串并将它们变为小写。

string = "0 is most powerful Number"

capitalizedString = string.capitalize()

print('Old String: ', string)
print('Capitalized String:', capitalizedString)

程序输出。

Old String:  0 is most powerful Number
Capitalized String: 0 is most powerful number

快乐学习!!

地址:https://www.cundage.com/article/python-string-capitalize.html

相关阅读

Learn to use Python 打印() function to 将 Python 程序或 Python 脚本的打印输出重定向到文件. 1. 使用 文件 参数打印到文件 打印()
在 Python 中,string.count() 用于计算给定输入字符串中字符或子字符串的出现次数。 input_string = "how to do in java" substri...
Python example to 将字符串拆分为 列表 的标记 using the delimiters such as space, comma, 正则表达式, or multiple de...
在 Python 中,capitalize() 方法将字符串大写,即大写给定字符串中的第一个字母,并将所有其他字符(如果有)小写。 除了改变大小写(大写/小写),capitalize() 不会修...
Python example to 将字符串拆分为 列表 的标记 using the delimiters such as space, comma, 正则表达式, or multiple de...
Learn to use Python 打印() function to 将 Python 程序或 Python 脚本的打印输出重定向到文件. 1. 使用 文件 参数打印到文件 打印() 函数接...
Python string.endswith() 用于检查特定文本模式的字符串结尾,例如域名后缀等。 1.字符串endswith()方法 检查字符串结尾的一种简单方法是使用 String.end...
Python bin() 方法将给定的 integer 转换为等效的二进制 < href="https://howtodoinjava.com/python-datatypes/pytho...
学习使用 Python httplib2 模块。 超文本传输协议 (HTTP) 是分布式协作超媒体信息系统的应用协议。 HTTP 是万维网数据通信的基础。 Python httplib2 模块提...
阅读、理解和练习这些 Python 示例,以更好地理解 Python 语言。这些简单的 Python 程序将帮助我们理解 Python 的基本编程概念。 此页面上的所有程序都经过测试,应该可以在...