How to change a string into uppercase

2019-01-07 02:28发布

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?

6条回答
甜甜的少女心
2楼-- · 2019-01-07 02:32

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'
查看更多
▲ chillily
3楼-- · 2019-01-07 02:42

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.

查看更多
淡お忘
4楼-- · 2019-01-07 02:44
>>> s = 'sdsd'
>>> s.upper()
'SDSD'

See String Methods.

查看更多
对你真心纯属浪费
5楼-- · 2019-01-07 02:52

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
查看更多
等我变得足够好
6楼-- · 2019-01-07 02:54
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()
查看更多
霸刀☆藐视天下
7楼-- · 2019-01-07 02:57

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.

查看更多
登录 后发表回答