当前位置: 动力学知识库 > 问答 > 编程问答 >

Deleting a particular character from a string in python

问题描述:

This question already has an answer here:

  • Python: Delete a character from a string

    3 answers

网友答案:

str objects don't support item deletion because they're immutable. The closest you can do is to make a new string w/out the last character by slicing the string:

In [20]: 'puete'[:-1]
Out[20]: 'puet'
网友答案:

Strings are immutable, i.e. can't be changed in-place; you cannot delete a single character. Instead, create a new string and assign it back to the same name:

s = "puete"
s = s[:-1]

The slice [:-1] means "up to but not including the last character", and will create a new string object with fewer characters.

分享给朋友:
您可能感兴趣的文章:
随机阅读: