Remove final character from string

0 votes
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.
Jan 5 in Python by Rajiv
• 8,890 points
109 views

1 answer to this question.

0 votes

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)
answered Jan 5 by Gitika
• 65,950 points

Related Questions In Python

0 votes
2 answers

Delete a character from pythons string

If you want to remove the 'J' ...READ MORE

answered Jun 12, 2018 in Python by anonymous
144 views
0 votes
1 answer

How to remove blank spaces from Python string?

You can use the strip method to do ...READ MORE

answered Apr 4, 2019 in Python by Rishi
419 views
0 votes
1 answer

print the character from string if character is alphabet

You can use str.isalpha() For example: s = 'a123b' for ...READ MORE

answered Sep 18, 2020 in Python by Roshni
• 10,480 points
100 views
0 votes
2 answers

Finding the index of a character in python string

You can use word.find('o') as well to ...READ MORE

answered Jun 1, 2018 in Python by george
• 200 points
538 views
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,810 points
1,810 views
0 votes
3 answers
0 votes
1 answer

Removing surrounding whitespace

Try the strip() function:  s = s.strip() If you ...READ MORE

answered May 4, 2018 in Python by Nietzsche's daemon
• 4,260 points
200 views
0 votes
1 answer

How to remove the white spaces from the string “aaa bbb ccc ddd eee”?

Hi, @Roshni, You can do it Using join, read ...READ MORE

answered Jun 26, 2020 in Python by Gitika
• 65,950 points
443 views
0 votes
1 answer

How to Remove specific characters from a string in Python?

Strings in Python are immutable (can't be changed). Because ...READ MORE

answered Jan 5 in Python by Gitika
• 65,950 points
137 views