Changing one character in a string

前端 未结 11 839
抹茶落季
抹茶落季 2020-11-22 03:21

What is the easiest way in Python to replace a character in a string?

For example:

text = \"abcdefg\";
text[1] = \"Z\";
           ^
11条回答
  •  有刺的猬
    2020-11-22 03:56

    This code is not mine. I couldn't recall the site form where, I took it. Interestingly, you can use this to replace one character or more with one or more charectors. Though this reply is very late, novices like me (anytime) might find it useful.

    Change Text function.

    mytext = 'Hello Zorld'
    mytext = mytext.replace('Z', 'W')
    print mytext,
    

提交回复
热议问题