Python: Delete a character from a string

前端 未结 3 710
深忆病人
深忆病人 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:25

    I use a function similar to :

    def delstring(mystring, indexes):
      return ''.join([let for ind, let in enumerate(mystring) if ind not in indexes])
    

    indexes should be an iterable (list, tuple..)

提交回复
热议问题