python 字符串转大写
Python String capitalize() function returns the capitalized version of the string. The first character of the returned string is converted to uppercase and rest of the characters are changed to lowercase.
Python字符串capitalize()函数返回字符串的大写版本。 返回的字符串的第一个字符转换为大写 ,其余字符更改为小写 。
Python字符串大写() (Python String capitalize())
Let’s look at an example of string capitalize() function.
让我们看一个字符串大写的例子。
s = 'WelCome to JournalDev'
print(s.capitalize())
Output: Welcome to journaldev
输出: Welcome to journaldev
Let’s look at another example where string first character is not an ASCII character.
让我们看另一个示例,其中字符串第一个字符不是ASCII字符。
s = '歡迎' # welcome in Chinese
print(s.capitalize())
s = 'çA†'
print(s.capitalize())
Output:
输出:
歡迎
Ça†
The Chinese capitalized version looks same as the original string. But I don’t know the Chinese language, so I am not sure if it’s correct or not.
中文大写版本与原始字符串相同。 但是我不懂中文,所以我不确定它是否正确。
For the special character, it looks like the capitalization worked and the first character got changed to uppercase.
对于特殊字符,看起来大写有效,并且第一个字符变为大写。
Finally, let’s look at an example of getting user input and capitalize the input string and print it.
最后,让我们看一个获取用户输入并大写输入字符串并将其打印的示例。
s = input('Please enter a string:\n')
print('Input String Capitalized Version:\n', s.capitalize())
>>> s = input('Please enter a string:\n')
Please enter a string:
apple çdžˇƒÏ
>>> print('Input String Capitalized Version:\n', s.capitalize())
Input String Capitalized Version:
Apple ç熡ƒï
>>>
Reference: Official Documentation
参考: 官方文档
翻译自: https://www.journaldev.com/23872/python-string-capitalize
python 字符串转大写