How to capitalize a string in Python? [duplicate]

冷暖自知 提交于 2020-12-30 07:10:21

问题


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?


回答1:


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.




回答2:


You're just missing the parentheses since Upper is a method. It should be "string".Upper().



来源:https://stackoverflow.com/questions/26574191/how-to-capitalize-a-string-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!