简体   繁体   English

python如何将字符串中的某些字符大写

[英]python how to uppercase some characters in string

Here is what I want to do but doesn't work:这是我想做但不起作用的事情:

mystring = "hello world"
toUpper = ['a', 'e', 'i', 'o', 'u', 'y']
array = list(mystring)

for c in array:
    if c in toUpper:
        c = c.upper()
print(array) 

"e" and "o" are not uppercase in my array. "e""o"在我的数组中不是大写的。

You can use the str.translate() method to have Python replace characters by other characters in one step.您可以使用str.translate()方法让 Python 在一个步骤中用其他字符替换字符。

Use the string.maketrans() function to map lowercase characters to their uppercase targets:使用string.maketrans()函数将小写字符映射到它们的大写目标:

try:
    # Python 2
    from string import maketrans
except ImportError:
    # Python 3 made maketrans a static method
    maketrans = str.maketrans 

vowels = 'aeiouy'
upper_map = maketrans(vowels, vowels.upper())
mystring.translate(upper_map)

This is the faster and more 'correct' way to replace certain characters in a string;这是替换字符串中某些字符的更快、更“正确”的方法; you can always turn the result of mystring.translate() into a list but I strongly suspect you wanted to end up with a string in the first place.你总是可以将mystring.translate()的结果转换成一个列表,但我强烈怀疑你首先想要得到一个字符串。

Demo:演示:

>>> try:
...     # Python 2
...     from string import maketrans
... except ImportError:
...     # Python 3 made maketrans a static method
...     maketrans = str.maketrans 
... 
>>> vowels = 'aeiouy'
>>> upper_map = maketrans(vowels, vowels.upper())
>>> mystring = "hello world"
>>> mystring.translate(upper_map)
'hEllO wOrld'

You are not making changes to the original list.您不会对原始列表进行更改。 You are making changes only to the loop variable c .您仅对循环变量c进行更改。 As a workaround you can try using enumerate .作为解决方法,您可以尝试使用enumerate

mystring = "hello world"
toUpper = ['a', 'e', 'i', 'o', 'u', 'y']
array = list(mystring)

for i,c in enumerate(array):
    if c in toUpper:
        array[i] = c.upper()

print(array) 

Output输出

['h', 'E', 'l', 'l', 'O', ' ', 'w', 'O', 'r', 'l', 'd']

Note: If you want hEllO wOrld as the answer, you might as well use join as in ''.join(array)注意:如果你想要hEllO wOrld作为答案,你不妨像在''.join(array)那样使用join

You can do:你可以做:

mystring = "hello world"
toUpper = ['a', 'e', 'i', 'o', 'u', 'y']

>>> ''.join([c.upper() if c in toUpper else c for c in mystring])
hEllO wOrld

像这样使用生成器表达式:

newstring = ''.join(c.upper() if c in toUpper else c for c in mystring)

The problem is that al c is not used for anything, this is not passing by reference.问题是 al c不用于任何事情,这不是通过引用传递。

I would do so, for beginners:对于初学者,我会这样做:

mystring = "hello world"
toUpper = ['a', 'e', 'i', 'o', 'u', 'y']
array = []
for c in mystring:
    if c in toUpper:
        c = c.upper()
    array.append(c)
print(''.join(array))

This will do the job.这将完成工作。 Keep in mind that strings are immutable, so you'll need to do some variation on building new strings to get this to work.请记住,字符串是不可变的,因此您需要在构建新字符串时进行一些更改才能使其正常工作。

myString = "hello world"
toUpper = ['a', 'e', 'i', 'o', 'u', 'y']
newString = reduce(lambda s, l: s.replace(l, l.upper()), toUpper, myString)

Please try this one请试试这个

mystring = "hello world"
toUpper = ['a', 'e', 'i', 'o', 'u', 'y']

array = list(mystring)
new_string = [x.upper() if x in toUpper else x for x in array ]



new_string = ''.join(new_string)
print new_string

Easy method简单的方法

name='india is my country and indians are my brothers and sisters'
vowles='a','e','i','o','u'
for name_1 in name:
    if name_1 in vowles:
        b=name_1.upper()
        print(b,end='')
    else:
        print(name_1,end='')

Here is code :这是代码:

    name='india is my country and indians are my brothers and sisters'
    vowles='a','e','i','o','u'
    for name_1 in name:
        if name_1 in vowles:
            b=name_1.upper()
            print(b,end='')
        else:
            print(name_1,end='')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM