python how to uppercase some characters in string

前端 未结 9 1401
北荒
北荒 2021-01-21 17:51

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 arr         


        
相关标签:
9条回答
  • 2021-01-21 18:29

    Use generator expression like so:

    newstring = ''.join(c.upper() if c in toUpper else c for c in mystring)
    
    0 讨论(0)
  • 2021-01-21 18:30

    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='')
    
    0 讨论(0)
  • 2021-01-21 18:32

    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
    
    0 讨论(0)
提交回复
热议问题