python how to uppercase some characters in string

前端 未结 9 1398
北荒
北荒 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: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
    

提交回复
热议问题