How to change a string into uppercase

PythonStringUppercase

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

Use str.upper():

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

See String Methods.

Solution 2 - Python

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'

Solution 3 - Python

to make the string upper case -- just simply type

s.upper()

simple and easy! you can do the same to make it lower too

s.lower()

etc.

Solution 4 - Python

s = 'sdsd'
print (s.upper())
upper = raw_input('type in something lowercase.')
lower = raw_input('type in the same thing caps lock.')
print upper.upper()
print lower.lower()

Solution 5 - Python

for making uppercase from lowercase to upper just use

"string".upper()

where "string" is your string that you want to convert uppercase

for this question concern it will like this:

s.upper()

for making lowercase from uppercase string just use

"string".lower()

where "string" is your string that you want to convert lowercase

for this question concern it will like this:

s.lower()

If you want to make your whole string variable use

s="sadf"
# sadf

s=s.upper()
# SADF

Solution 6 - Python

For questions on simple string manipulation the dir built-in function comes in handy. It gives you, among others, a list of methods of the argument, e.g., dir(s) returns a list containing upper.

Solution 7 - Python

For converting first letter of each word into capital in a sentence

s = 'this is a sentence'

str.title(s)

>>> 'This Is A Sentence'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongadssView Question on Stackoverflow
Solution 1 - PythonDan D.View Answer on Stackoverflow
Solution 2 - PythonKL-7View Answer on Stackoverflow
Solution 3 - PythonKatie TView Answer on Stackoverflow
Solution 4 - PythonH CODEView Answer on Stackoverflow
Solution 5 - PythonPawanvir singhView Answer on Stackoverflow
Solution 6 - PythonbartfrenkView Answer on Stackoverflow
Solution 7 - PythoncoderinaView Answer on Stackoverflow