Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change a string into uppercase

I have problem in changing a string into uppercase with Python. In my research, I got string.ascii_uppercase but it doesn't work.

The following code:

 >>s = 'sdsd'  >>s.ascii_uppercase 

Gives this error message:

Traceback (most recent call last):   File "<console>", line 1, in <module> AttributeError: 'str' object has no attribute 'ascii_uppercase' 

My question is: how can I convert a string into uppercase in Python?

like image 594
gadss Avatar asked Feb 13 '12 07:02

gadss


People also ask

How do you convert a string to uppercase?

Java String toUpperCase() MethodThe toUpperCase() method converts a string to upper case letters. Note: The toLowerCase() method converts a string to lower case letters.

How do you change a lowercase string into uppercase?

Program to convert the uppercase string into lowercase using the strlwr() function. Strlwr() function: The strlwr() function is a predefined function of the string library used to convert the uppercase string into the lowercase by passing the given string as an argument to return the lowercase string.


2 Answers

Use str.upper():

>>> s = 'sdsd' >>> s.upper() 'SDSD' 

See String Methods.

like image 74
Dan D. Avatar answered Oct 02 '22 19:10

Dan D.


To get upper case version of a string you can use str.upper:

s = 'sdsd' s.upper() #=> 'SDSD' 

On the other hand string.ascii_uppercase is a string containing all ASCII letters in upper case:

import string string.ascii_uppercase #=> 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 
like image 26
KL-7 Avatar answered Oct 02 '22 20:10

KL-7