python仅将首字母大写 [英] python capitalize first letter only
本文介绍了python仅将首字母大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道.capitalize()将字符串的第一个字母大写,但是如果第一个字符是整数呢?
I am aware .capitalize() capitalizes the first letter of a string but what if the first character is a integer?
this
1bob
5sandy
为此
1Bob
5Sandy
推荐答案
如果第一个字符是整数,则不会大写第一个字母。
If the first character is an integer, it will not capitalize the first letter.
>>> '2s'.capitalize()
'2s'
如果需要此功能,请剥离您可以使用'2'.isdigit()
来检查每个字符。
If you want the functionality, strip off the digits, you can use '2'.isdigit()
to check for each character.
>>> s = '123sa'
>>> for i, c in enumerate(s):
... if not c.isdigit():
... break
...
>>> s[:i] + s[i:].capitalize()
'123Sa'
这篇关于python仅将首字母大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文