# python – Remove final character from string

## The Question :

567 people think this question is useful

Let’s say my string is 10 characters long.

How do I remove the last character?

If my string is "abcdefghij" (I do not want to replace the 'j' character, since my string may contain multiple 'j' characters) I only want the last character gone. Regardless of what it is or how many times it occurs, I need to remove the last character from my string.

The Question Comments :
• I don’t believe this to be a duplicate. In the other question, the OP asked for a substring in general. This OP asks for a very specific substring which is often very useful. I believe having this here for people to search AND for people to answer is useful. It would be nice for someone who may want 1) to take off a trailing os separator character from a bunch of paths or 2) to remove a last comma on each line of a CSV which has an empty last column or 3) to remove a trailing period/full stop (any punctuation) from the end of a bunch of sentence strings … [more examples, no chars]
• Especially when one is new to programming, asking one to figure out the my_str[:-1] from the answers in the dup link seems a bit of a jump. As the linked site appears RIGHT NOW (see the lynx command), it’s hard to find. \$ lynx -dump https://web.archive.org/web/20200826203245/https://stackoverflow.com/questions/663171/how-do-i-get-a-substring-of-a-string-in-python | grep -n "$[:][-]1$" \n 540: print(a[:-1]) \n 542: In the above code, [:-1] declares to print from the starting till the \n 548:  Note: Here a [:-1] is also the same as a [0:-1] and a [0:len(a)-1]

## The Answer 1

944 people think this answer is useful

Simple:

st =  "abcdefghij"
st = st[:-1]



There is also another way that shows how it is done with steps:

list1 = "abcdefghij"
list2 = list(list1)
print(list2)
list3 = list2[:-1]
print(list3)



This is also a way with user input:

list1 = input ("Enter :")
list2 = list(list1)
print(list2)
list3 = list2[:-1]
print(list3)



To make it take away the last word in a list:

list1 = input("Enter :")
list2 = list1.split()
print(list2)
list3 = list2[:-1]
print(list3)



## The Answer 2

37 people think this answer is useful

What you are trying to do is an extension of string slicing in Python:

Say all strings are of length 10, last char to be removed:

>>> st[:9]
'abcdefghi'



To remove last N characters:

>>> N = 3
>>> st[:-N]
'abcdefg'

`