Strings are iterable objects. This means you can access their values using indexing and slicing. These techniques allow you to retrieve an individual character or range of characters from a string.
In this guide, we discuss how to remove the last character from a Python string. We’ll walk through an example of removing the last character from a string so you can figure out how you can accomplish this task in your code.
Python: Remove Last Character from String
We want to build a program that removes the last character from an employee identifier. This character tells us the department for which an employee works.
For instance, the value “M” tells us an employee works for the marketing department. We are going to remove this character because it is being replaced by a new system to keep track of the department for which an employee works.
Let’s start by asking the user to insert an employee identifier using the input() method:
identifier = input("What is the employee's identifier? ")
Next, we remove the last character from the identifier:
new_identifier = identifier[:-1]
The [:-1] is a string slice operation that removes the last character from the list. We use negative indexing to retrieve items from the end of the string. Now, display an employee’s new identifier to the console:
print("The employee's new identifier is {}.".format(new_identifier))
The .format() statement replaces the curly braces inside our string with the employee’s new identifier. Let’s run our code and see what happens:
What is the employee's identifier? 28371M The employee's new identifier is 28371.
Our program removes the last value in an employee’s identifier.
We’re not done yet. If a user inserts a blank value, our program returns:
The employee's new identifier is .
We should make this more graceful. To do so, we need to slice an employee’s identifier if the identifier has more than two characters. Otherwise, our program will inform the user they need to insert a valid identifier.
Let’s define an “if” statement that checks for the length of an identifier:
identifier = input("What is the employee's identifier? ") if len(identifier) > 1: new_identifier = identifier[:-1] print("The employee's new identifier is {}.".format(new_identifier)) else: print("Please insert a valid identifier.")
We use the len()
method to find out the length of an employee’s identifier. If the length of that identifier is greater than “1”, our if
statement executes. Otherwise, the “else” statement runs.
Let’s run our Python program and specify an identifier that is less than two characters long:
What is the employee's identifier? B Please insert a valid identifier.
Our code informs us we need to choose a valid identifier.
Conclusion
Slicing syntax lets you delete the last character from a string object in Python. All you need to do is specify a string and add [:-1] after the string. Now you’re ready to remove the last character from a Python string like an expert!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.