问题:如何将字符串转换为大写

我在使用Python将字符串更改为大写时遇到问题。在我的研究中,我知道了,string.ascii_uppercase但是没有用。

如下代码:

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

给出此错误信息:

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

我的问题是:如何在Python中将字符串转换为大写?

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?


回答 0

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

请参阅字符串方法

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

See String Methods.


回答 1

要获取字符串的大写版本,可以使用str.upper

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

另一方面,string.ascii_uppercase是一个包含所有大写ASCII字母的字符串:

import string
string.ascii_uppercase
#=> 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

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'

回答 2

使字符串大写-只需键入

s.upper()

简单容易!你也可以做同样的事情来降低它

s.lower()

等等

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.


回答 3

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()
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()

回答 4

用于将大写字母从小写字母转换为大写字母

"string".upper()

"string"您要转换大写的字符串在哪里

对于这个问题,它会像这样:

s.upper()

用于从大写字符串制作小写字母,只需使用

"string".lower()

"string"您要转换小写的字符串在哪里

对于这个问题,它会像这样:

s.lower()

如果要使用整个字符串变量

s="sadf"
# sadf

s=s.upper()
# SADF

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

对于有关简单字符串操作的问题,dir内置函数非常方便。它给您提供参数方法的列表,例如,dir(s)返回包含的列表upper

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.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。