795

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?

Nope
  • 21,907
  • 7
  • 45
  • 72
gadss
  • 20,175
  • 36
  • 98
  • 147

7 Answers7

1314

Use str.upper():

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

See String Methods.

Tomerikoo
  • 15,737
  • 15
  • 35
  • 52
Dan D.
  • 70,581
  • 13
  • 96
  • 116
96

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'
KL-7
  • 43,332
  • 9
  • 85
  • 74
19

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.

Katie T
  • 341
  • 2
  • 6
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()
Community
  • 1
  • 1
H CODE
  • 199
  • 1
  • 2
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
Keyur Potdar
  • 6,949
  • 6
  • 25
  • 39
Pawanvir singh
  • 373
  • 6
  • 17
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.

bartfrenk
  • 103
  • 3
  • 4
1

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

s = 'this is a sentence'

str.title(s)

>>> 'This Is A Sentence'
coderina
  • 1,205
  • 10
  • 18