Python: Delete a character from a string

前端 未结 3 677
深忆病人
深忆病人 2021-01-23 03:34

I want to delete i.e. character number 5 in a string. So I did:

del line[5]

and got: TypeError: \'str\' object doesn\'t support item deletion

So no I won

3条回答
  •  囚心锁ツ
    2021-01-23 04:00

    bytearray is a type which can be changed in place. And if you are using Python2.x, it can very easily convert to default str type: bytes.

    b=bytearray(s)
    del b[5]
    s=str(b)
    

提交回复
热议问题