How to Remove the Last Two Characters from a String in Python
In Python, strings can be shortened by removing the last two characters. This is done by using the string slice operator, which takes two arguments: ...

In Python, strings can be shortened by removing the last two characters. This is done by using the string slice operator, which takes two arguments: the start and end position of the slice.
For example, to remove the last two characters from a string "Hello, world!", you would use the string slice operator as follows:
"Hello, world!"[-2:]
- By slicing, remove the last N characters from the string.
- Slicing is used to remove the last three characters from a string.
- Slicing is used to remove the last character from a string.
- Using negative slicing, remove the last N characters from a string.
- Using a for loop, remove the last N characters from a string.
- Using regex, remove the last N characters from a string.
- If a comma is present, remove the last character from the string.
- Pandas Tutorials - Learn Python Data Analysis
- Are you interested in a career in Data Science with Python?
- In Python, how do you remove the last two characters of a string?
- How do I get rid of the last two letters in a string?
- In Python, how do I remove the last three characters from a string?
- How do I get rid of the last three characters in a string?
In this article, we will look at various methods for removing the last N characters from a string.e. slicing, for looping, or regex.
By slicing, remove the last N characters from the string.
We can use slicing in Python to select a specific range of characters in a string.e.
str[start:end]
It returns the characters of the string as a new string from index position start to end -1. start default values. Where Z is the string's length.
If neither start nor end are specified, it selects all characters in the string i.e. from 0 to size-1 and generates a new string based on those characters.
Advertisements
We can use this slicing technique to extract a piece of string that contains all characters except the last N characters. Let's see how we can do that.
Slicing is used to remove the last three characters from a string.
Assume our string has N characters. We'll cut the string to select characters from index position 0 to N-3 and then assign them to the variable i.e.
org_string = "Sample String" size = len(org_string) # Slice string to remove last 3 characters from string mod_string = org_string[:size - 3] print(mod_string)
Output
Sample Str
By selecting characters in string from index position 0 to N-3, we selected all characters from string except the last three characters and then assigned this sliced string back to the same variable, giving the impression that the last three characters in string are deleted.
P.S. We did not explicitly specify 0 as the start index because it is the default value. So we just skipped it.
Slicing is used to remove the last character from a string.
org_string = "Sample String" size = len(org_string) # Slice string to remove last character from string mod_string = org_string[:size - 1] print(mod_string)
Output
Sample Strin
We chose all characters in the string except the last one by selecting characters from index position 0 to size-1.
The main disadvantage of using positive indexing in slicing is: First, we determined the length of string i.e. N, then cut the string from 0 to N-1. What if we don't want to calculate the length of the string but still want to delete the characters at the end?
Using negative slicing, remove the last N characters from a string.
We can also use negative indexing to select characters in a string in Python. The last character in the string has index -1, and it will continue to decrease until we reach the beginning of the string.
For example for string “Sample”,
The index of the character 'e' is -1.
The character 'l' has a -2 index.
The index of the character 'p' is -3.
The character'm' has a -4 index.
Character 'a' has a -5 index.
The character 'S' has a -6 index.
So, to remove the last three characters from a string, choose a character from 0 i.e. to -3 i.e.
org_string = "Sample String" # Slice string to remove 3 last characters mod_string = org_string[:-3] print(mod_string)
Output
Sample Str
It returned a new string constructed with characters from the beginning of the given string until index -4 i.e. all but the last three characters of the string. We returned it to the string to have the effect of deleting the last three characters of the string.
Negative slicing is used to remove the last characters from a string.
To remove the last character from a string, slice it to select characters from the beginning to index position -1 i.e.
org_string = "Sample String" # Slice string to remove 1 last character mod_string = org_string[:-1] print(mod_string)
Output
Sample Strin
It chose all of the characters in the string except the last one.
Using a for loop, remove the last N characters from a string.
We can iterate through the string's characters one by one, selecting all characters from the beginning to the (N -1)th character. Let's say we want to remove the last three characters from a string i.e.
org_string = "Sample String" n = 3 mod_string = "" # Iterate over the characters in string and select all except last 3 for i in range(len(org_string) - n): mod_string = mod_string + org_string[i] print(mod_string)
Output
Sample Str
Simply set n to 1 to remove the last character.
Using regex, remove the last N characters from a string.
In Python, we can use regex to match two groups in a string.e.
- Group 1: Except for the last N characters, every character in the string
- Group 2: Last N character of string
The string is then modified by replacing both groups with a single group i.e. group 1.
For example, let's use regex to remove the last three characters from a string.
org_string = "Sample String" size = len(org_string) # Slice string to remove last 3 characters from string mod_string = org_string[:size - 3] print(mod_string)1
Output
Sample Str
In this case, the sub() function matched the specified pattern in the string and returned the matched object to our call-back function remove_second_group(). The Match object has two groups internally, and the function remove_second_group() returned a string by only selecting characters from group 1. Then, using the sub() function, the matched characters in string were replaced with the characters returned by the remove_second_group() function.
Use 1 instead of 0 to remove the last character from a string using regex.
org_string = "Sample String" size = len(org_string) # Slice string to remove last 3 characters from string mod_string = org_string[:size - 3] print(mod_string)3
Output
Sample Strin
It removed the string's final character.
If a comma is present, remove the last character from the string.
In some cases, we may want to delete the last character of a string only if it meets certain criteria. Let's see how we can do that.
If the last character in the string is a comma, remove it.e.
org_string = "Sample String" size = len(org_string) # Slice string to remove last 3 characters from string mod_string = org_string[:size - 3] print(mod_string)5
Output:
org_string = "Sample String" size = len(org_string) # Slice string to remove last 3 characters from string mod_string = org_string[:size - 3] print(mod_string)6
So, using regex and other techniques, we can delete the last N characters from a string in Python.
The full example is as follows:
org_string = "Sample String" size = len(org_string) # Slice string to remove last 3 characters from string mod_string = org_string[:size - 3] print(mod_string)7
Output:
org_string = "Sample String" size = len(org_string) # Slice string to remove last 3 characters from string mod_string = org_string[:size - 3] print(mod_string)8
Pandas Tutorials - Learn Python Data Analysis
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
Pandas Tutorial Part #2 - Basics of Pandas Series
Pandas Tutorial Part #3 - Get & Set Series values
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
Pandas Tutorial Part #6 - Introduction to DataFrame
Pandas Tutorial Part #7 - DataFrame.loc[] - Indexed Rows / Columns Selection
Pandas Tutorial Part #8 - DataFrame.iloc[] - Sort Rows / Columns based on Label Names
Pandas Tutorial Part #9 - Filter DataFrame Rows
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
Pandas Tutorial Part #11 - DataFrame attributes & methods
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
Are you interested in a career in Data Science with Python?
Data Science is the future, and it is already here. Data Scientists are now the most in-demand professionals. To become a good Data Scientist or to change careers in Data Science, one must have the necessary skill set. We've compiled a list of the Top Professional Certificate in Data Science with Python. These courses will teach you how to use data science programming tools such as Pandas, NumPy, Matplotlib, and Seaborn, as well as how to use these libraries to implement machine learning models.
Check out our in-depth analysis of the Best Professional Certificate in Data Science with Python.
Remember that Data Science necessitates a great deal of patience, persistence, and practice. So, start learning today.