python 字符串转大写_Python字符串大写()

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 ç熡ƒï
>>>
GitHub Repository. GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/23872/python-string-capitalize

python 字符串转大写

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值