Working with strings is an essential skill for a Python developer of any skill level. In many cases, this means cleaning strings, such as trimming either the front or end of a string. In this tutorial, you’ll learn how to remove the first or last character from a Python string. Similarly, you’ll learn how to remove the last word from a string.
Table of Contents
Understanding String Indexing in Python
Python strings are ordered and indexed, meaning that we can access items from a string by their position. Similar to other indexed objects in Python, such as lists and tuples, Python strings can also be indexed using slices. Let’s take a quick look at what indexing and slicing strings looks like:
# Understanding Python String Indexing and Slicing
a_string = 'Welcome to datagy.io'
first_character = a_string[0]
last_character = a_string[-1]
first_two_characters = a_string[:2]
last_two_characters = a_string[-2:]
print('First character: ', first_character)
print('Last character: ', last_character)
print('First two characters: ', first_two_characters)
print('Last two characters: ', last_two_characters)
# Returns:
# First character: W
# Last character: o
# First two characters: We
# Last two characters: io
There are two key takeaways from the code block above:
- Strings can have items accessed using position and negative indexing
- Strings can be sliced using Python slicing, both from a positive and negative direction
With these two takeaways in mind, let’s dive into how we can apply this knowledge to remove characters from a string.
Remove the First Character From a Python String
To remove the first character from a Python string, you can simply reassign a sliced version of the string to itself. Because Python strings are immutable, we can’t modify the string directly. Instead, we need to destroy the object and recreated it using a slice.
Let’s see how we can drop the first character from a Python string:
# Remove the First Character From a Python String
a_string = 'Welcome to datagy.io'
a_string = a_string[1:]
print(a_string)
# Returns: elcome to datagy.io
In the code block above, we slice from the second character to the end. This slice is then assigned to the original variable, thereby removing the first character from the string.
Remove the First n Characters From a Python String
To remove the first n number of characters from a Python string, we simply need to expand the scope of our slice. Say we wanted to remove the first three characters from a string, we would simply begin the slice at index 3.
Let’s see how we can drop the first 3 characters from a string in Python:
# Remove the First n Character From a Python String
a_string = 'Welcome to datagy.io'
n = 3
a_string = a_string[n:]
print(a_string)
# Returns: come to datagy.io
We can see that starting the slice at n, we were able to drop the first n characters from a Python string. In the following section, you’ll learn how to remove the last character from a Python string.
Remove the Last Character From a Python String
Because Python list items can be accessed using negative indices, we can remove the last character from a string by using its negative indices. This provides simplifies the process significantly over using positive indices, since we don’t need to know how long the string is.
In order to remove the last character from a Python string, we can slice the string up to the last character and assign the string back to itself. Let’s see what this looks:
# Remove the Last Character From a Python String
a_string = 'Welcome to datagy.io'
a_string = a_string[:-1]
print(a_string)
# Returns: Welcome to datagy.i
We can see that by indexing up to the last index position, that we were able to remove the last character from a string in Python.
Remove the Last n Characters From a Python String
Similar to dropping the last character of a Python string, we can use negative slicing to drop the last n character from a string in Python. In this case, we simply slice from the negative index to the end. Let’s see how we can remove the last three characters from a string:
# Remove the Last n Character From a Python String
a_string = 'Welcome to datagy.io'
n = 3
a_string = a_string[:-n]
print(a_string)
# Returns: come to datagy.io
We can see that in this case we simply had to slice from the negative index position of the number of characters we wanted to remove. In the following section, you’ll learn how to apply what you’ve learned by removing the first word from a Python string.
Remove the First Word From a Python String
Removing a word from a Python string works a bit differently. We can first split the string using a delimiter, such as a space. From there, we can drop the word using slicing. Then, we can easily join the string back together using the .join()
method.
Let’s see how we can drop the first word from our string:
# Remove the First Word From a String
a_string = 'Welcome to datagy.io'
a_string = ' '.join(a_string.split(' ')[1:])
print(a_string)
# Returns: to datagy.io
Let’s break down what we did in the code block above:
- We split the string using the ‘ ‘ delimiter.
- We then dropped the first item from the resulting list.
- We then joined the string back together using the
.join()
method
Let’s see how we can use this method to instead drop the last word from a string.
Remove the Last Word From a Python String
In order to remove the last word from a Python string, we can repeat the same method as above. However, instead of slicing the list to remove the first element, we remove the last. Let’s see what this looks like in Python:
# Remove the Last Word From a String
a_string = 'Welcome to datagy.io'
a_string = ' '.join(a_string.split(' ')[:-1])
print(a_string)
# Returns: Welcome to
Conclusion
In this post, you learned how to remove the first or last character from a Python string. You also learned how to remove the first or last n characters from a Python string. You learned how to use string indexing and slicing in order to accomplish this. From there, you learned how to apply this to remove the first and last word from a string.
Additional Resources
To learn more about related topics, check out the tutorials below: