Changing one character in a string

前端 未结 11 1029
抹茶落季
抹茶落季 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 04:08

    if your world is 100% ascii/utf-8(a lot of use cases fit in that box):

    b = bytearray(s, 'utf-8')
    # process - e.g., lowercasing: 
    #    b[0] = b[i+1] - 32
    s = str(b, 'utf-8')
    

    python 3.7.3

提交回复
热议问题