0

Let's say I have a string "puete" and I want to delete the last "e", how can I do it?

I have used del[len(str)-1] but there is error showing 'str does not support item deletion'.

0

2 Answers 2

3

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'
Sign up to request clarification or add additional context in comments.

1 Comment

can you elaborate you solution
2

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.