In this tutorial, we will look at how to remove the last n characters from a string in Python with the help of some examples.
How to remove the last n characters from a string?
Python strings are immutable which means that they cannot be modified. However, you can create a copy of the string with the last n characters removed and assign it to the original string variable.
To remove the last n characters from a string, slice the string from its start to the index of the nth character from the end. Using negative indexing can be helpful here. The following is the syntax –
# remove last n characters from a string s = s[:-n]
It returns a copy of the original string with the last n characters removed.
Let’s look at some examples -.
Remove the last 2 characters from a string
In this case n = 2, hence we slice the string from its starting index (which is 0) to -2 (which represents the index of the 2nd last character in the string).
# create a string s = "THIS IS SPARTAAA" # remove last 2 characters print(s[:-2])
Output:
THIS IS SPARTA
You can see that the resulting string does not have the last two characters from the original string.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Remove the last 3 characters from a string
We can similarly use the above syntax to remove the last three characters from a string.
# create a string s = "THIS IS SPARTAAA" # remove last 3 characters print(s[:-3])
Output:
THIS IS SPART
The resulting string has the last three characters removed.
You might also be interested in –
- Remove Last Character From String in Python
- Remove First Character From String in Python
- Python – Remove Consecutive Duplicates From String
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.