813

How can I convert a string into uppercase in Python?

When I tried to research the problem, I found something about string.ascii_uppercase, but it couldn't solve the problem:

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

See How do I lowercase a string in Python? for the opposite.

2

8 Answers 8

1333

Use str.upper():

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

See String Methods.

4
  • 167
    Also worth mentioning title(), 'abc def'.title() will give you Abc Def Commented Jan 14, 2014 at 12:34
  • 1
    It works for char type as well. Thank you for your helpful answer. Commented Jan 16, 2016 at 14:01
  • 2
    @yvesBaumes what do you mean by "char type"? Python does not have chars. Only strings with length of 1 Commented Jul 26, 2019 at 19:19
  • 2
    Please Note: The .upper() and .lower() functions do not modify the original str i.e. use s = s.upper() for effective results
    – Chaitanya
    Commented Jan 8, 2020 at 23:21
100

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'
20

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.

18
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()
1
  • 10
    Welcome to Stack Overflow @HCode! It is customary to add some commentary to your code. Commented Jul 16, 2014 at 2:57
7

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
5

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.

1

For converting the string into uppercase

s = 'capital letters'

s.upper()

>>> 'CAPITAL LETTERS'

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

s = 'this is a sentence'

str.title(s)

>>> 'This Is A Sentence'
0
-1

You can use capitalize() to make the 1st letter uppercase as shown below:

test = "this is a sentence."

print(test.capitalize()) # Here

Output:

This is a sentence.
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.