Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capitalize a string in Python? [duplicate]

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?

like image 841
A.. Avatar asked Oct 26 '14 14:10

A..


People also ask

How do you capitalize strings in Python?

Python String capitalize()The capitalize() method converts the first character of a string to an uppercase letter and all other alphabets to lowercase.

How do you capitalize a word in Python?

To capitalize the first letter, use the capitalize() function. This functions converts the first character to uppercase and converts the remaining characters to lowercase. It doesn't take any parameters and returns the copy of the modified string.

What does capitalize () mean in Python?

The capitalize() method returns a string where the first character is upper case, and the rest is lower case.

How do you make the first letter of a string uppercase in Python?

How do you capitalize the first letter of a string? The first letter of a string can be capitalized using the capitalize() function. This method returns a string with the first letter capitalized. If you are looking to capitalize the first letter of the entire string the title() function should be used.


1 Answers

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.

like image 156
eric Avatar answered Oct 21 '22 18:10

eric