Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove final character from string

Tags:

python

string

How do I remove the last character from a string?

"abcdefghij"  →  "abcdefghi"
like image 269
user1675111 Avatar asked Mar 18 '13 13:03

user1675111


People also ask

How do you remove the last character of a string?

Using String. The easiest way is to use the built-in substring() method of the String class. In order to remove the last character of a given String, we have to use two parameters: 0 as the starting index, and the index of the penultimate character.

How do I remove the last word from a string in Java?

To remove the last word from a string, get the index of the last space in the string, using the lastIndexOf() method. Then use the substring() method to get a portion of the string with the last word removed.

How do I remove the last character of a string in C++?

Use pop_back() Function to Remove Last Character From the String in C++ The pop_back() is a built-in function in C++ STL that removes the last element from a string. It simply deletes the last element and adjusts the length of the string accordingly.

How do I remove the last 3 characters from a string?

slice() method to remove the last 3 characters from a string, e.g. const withoutLast3 = str. slice(0, -3); . The slice method will return a new string that doesn't contain the last 3 characters of the original string.


5 Answers

Simple:

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

Try the following code snippet to better understand how it works by casting the string as a list:

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

In case, you want to accept the string from the user:

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

To make it take away the last word from a sentence (with words separated by whitespace like space):

str1 = input("Enter :")
list1 = str1.split()
print(list1)
list2 = list1[:-1]
print(list2)
like image 181
Cyrille Avatar answered Oct 12 '22 23:10

Cyrille


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'
like image 31
Anshul Goyal Avatar answered Oct 13 '22 00:10

Anshul Goyal


The simplest solution for you is using string slicing.

Python 2/3:

source[0: -1]  # gets all string but not last char

Python 2:

source = 'ABC'    
result = "{}{}".format({source[0: -1], 'D')
print(result)  # ABD

Python 3:

source = 'ABC'    
result = f"{source[0: -1]}D"
print(result)  # ABD
like image 45
Nico Avatar answered Oct 13 '22 01:10

Nico


So there is a function called rstrip() for stuff like this. You enter the value you want to delete, in this case last element so string[-1] :

string = "AbCdEf" 
newString = string.rstrip(string[-1])
print(newString)

If you runt his code you shouul see the 'f' value is deleted.

OUTPUT: AbCdE
like image 31
Arslanex Avatar answered Oct 13 '22 01:10

Arslanex


Using slicing, one can specify the start and stop indexes to extract part of a string s. The format is s[start:stop]. However, start = 0 by default. So, we only need to specify stop.


Using stop = 3:

>>> s = "abcd"
>>> s[:3]
'abc'

Using stop = -1 to remove 1 character from the end (BEST METHOD):

>>> s = "abcd"
>>> s[:-1]
'abc'

Using stop = len(s) - 1:

>>> s = "abcd"
>>> s[:len(s) - 1]
'abc'
like image 36
Mateen Ulhaq Avatar answered Oct 13 '22 01:10

Mateen Ulhaq