如何在 Python 中将字符串大写? [英] How to capitalize a string in Python?
本文介绍了如何在 Python 中将字符串大写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 Python 3.4 中将字符串转换为全部大写?
例如,我想转换:
字符串
到:
STRING
我尝试了 .upper
方法,但它返回:
"string".upper<0x0283E860处str对象的内置方法上层>
我该如何解决这个问题?
解决方案
您可以使用 string.upper() 方法
例如
<预><代码>>>>x = 'abcdef'>>>x.upper()>>>'ABCDEF'或者如果只需要首字母大写,可以使用 string.capitalize() 方法如
<预><代码>>>>x = 'abcdef'>>>x.capitalize()>>>'ABCDEF'希望有帮助.
How can I convert a string to all capitals in Python 3.4?
for example, I want to convert:
string
to:
STRING
I have tried with the .upper
method, but it returns:
"string".upper
<built-in method upper of str object at 0x0283E860>
How can I fix this problem?
解决方案
You can use string.upper() method in Python 3.4
For example
>>> x = 'abcdef'
>>> x.upper()
>>> 'ABCDEF'
Or if you only need the first letter to be capitalized, you can use string.capitalize() method like
>>> x = 'abcdef'
>>> x.capitalize()
>>> 'Abcdef'
Hope it helps.
这篇关于如何在 Python 中将字符串大写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文