python how to uppercase some characters in string

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

    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)
    

提交回复
热议问题