Last updated: Apr 9, 2024
Reading timeยท6 min
To remove everything after a character in a string:
str.split()
method to split the string on the separator.0
to get everything before the separator.my_str = 'bobby!hadz!com' separator = '!' result = my_str.split(separator, 1)[0] print(result) # ๐๏ธ 'bobby'
Note: if you need to remove everything BEFORE a character, click on the following subheading:
We used the str.split()
method to remove everything after a character (!
in
the example).
The str.split() method splits the string into a list of substrings using a delimiter.
The method takes the following 2 parameters:
Name | Description |
---|---|
separator | Split the string into substrings on each occurrence of the separator |
maxsplit | At most maxsplit splits are done (optional) |
If the separator is not found in the string, a list containing only 1 element is returned.
maxsplit
argument to 1
because we only need to split the string once.The example removes everything after the first occurrence of the character in the string.
my_str = 'bobby!hadz!com' separator = '!' result_1 = my_str.split(separator, 1)[0] print(result_1) # ๐๏ธ 'bobby' # ๐๏ธ ['bobby', 'hadz!com'] print(my_str.split(separator, 1))
Notice that the separator is not included in the string. If you need to include it, use the addition (+) operator.
my_str = 'bobby!hadz!com' # โ Remove everything after a character, keeping the separator separator = '!' result = my_str.split(separator, 1)[0] + separator print(result) # ๐๏ธ bobby!
The addition (+) operator can be used to concatenate strings in Python.
Note: if you need to remove everything BEFORE a character, click on the following subheading:
If you need to remove everything after the last occurrence of the character in
the string, use the str.rsplit()
method.
my_str = 'bobby!hadz!com' separator = '!' # โ Remove everything after the LAST occurrence of character result = my_str.rsplit(separator, 1)[0] print(result) # ๐๏ธ 'bobby!hadz'
Except for splitting from the right, rsplit()
behaves like split()
.
str.rsplit()
method splits the string from the right, and with maxsplit
set to 1
, it only splits once.If you need to include the character you split on, use the addition operator (+).
my_str = 'bobby!hadz!com' separator = '!' result = my_str.rsplit(separator, 1)[0] + separator print(result) # ๐๏ธ 'bobby!hadz!'
You can also use the str.partition()
method to remove everything after a
specific character in a string.
my_str = 'bobby!hadz!com' separator = '!' result = my_str.partition(separator)[0] print(result) # ๐๏ธ 'bobby' result = ''.join(my_str.partition(separator)[0:2]) print(result) # ๐๏ธ 'bobby!'
The str.partition() method splits the string at the first occurrence of the provided separator.
The method returns a tuple containing 3 elements - the part before the separator, the separator, and the part after the separator.
my_str = 'bobby!hadz!com' separator = '!' # ๐๏ธ ('bobby', '!', 'hadz!com') print(my_str.partition(separator))
If you need to include the separator in the result, use the str.join()
method
to join the first and second list items.
my_str = 'bobby!hadz!com' separator = '!' result = ''.join(my_str.partition(separator)[0:2]) print(result) # ๐๏ธ 'bobby!'
The str.join() method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.
The string the method is called on is used as the separator between the elements.
To remove everything before a character in a string:
str.find()
method to get the index of the character.start
index to the index of the character.my_str = 'apple, banana' result = my_str[my_str.find('b'):] print(result) # ๐๏ธ banana
The str.find
method returns the index of the first occurrence of the provided
substring in the string.
We used string slicing to get a part of the original string that starts at the index of the character and continues to the end of the string.
Note that the str.find()
method returns -1
if the substring is not found in
the string.
You can handle the scenario where the find()
method returns -1
in an
if/else
statement.
my_str = 'apple, banana' index = my_str.find('b') print(index) # ๐๏ธ 7 if index != -1: result = my_str[index:] else: result = my_str # ๐๏ธ alternatively raise an error print(result) # ๐๏ธ 'banana'
And, here is an example of a case where the provided character is not in the string.
my_str = 'apple, banana' index = my_str.find('z') print(index) # ๐๏ธ -1 if index != -1: result = my_str[index:] else: result = my_str # ๐๏ธ alternatively raise an error print(result) # ๐๏ธ 'apple, banana'
Our else
statement assigns the result
variable to the entire string,
however, you could also raise an exception.
my_str = 'apple, banana' index = my_str.find('z') print(index) # ๐๏ธ -1 if index != -1: result = my_str[index:] else: # ๐๏ธ this runs raise IndexError('provided character not in string')
If you need to remove everything before the last occurrence of a character, use
the str.rfind()
method.
my_str = 'apple,banana,bear' result = my_str[my_str.rfind('b'):] print(result) # ๐๏ธ 'bear'
The str.rfind() method returns the highest index in the string where the provided substring is found.
The method returns -1
if the substring is not contained in the string.
You can handle the scenario where the character is not present in the string
with an if/else
statement.
my_str = 'apple,banana,bear' index = my_str.rfind('b') if index != -1: result = my_str[index:] else: result = my_str print(result) # ๐๏ธ 'bear'
If the else
block runs, we set the result
variable to the entire string.
Alternatively, you can raise an error in the else
block, e.g.
raise IndexError('your message here')
.
This is a three-step process:
str.rsplit()
method to split the string from the right.1
.my_str = 'example.com/articles/python' result = my_str.rsplit('/', 1)[1] print(result) # ๐๏ธ 'python' # ๐๏ธ if you want to include the character in the result result_2 = '/' + my_str.rsplit('/', 1)[1] print(result_2) # ๐๏ธ '/python' # ๐๏ธ ['example.com/articles', 'python'] print(my_str.rsplit('/', 1))
We used the str.rsplit()
method to remove everything before the last
occurrence of a character.
The str.rsplit() method returns a list of the words in the string using the provided separator as the delimiter string.
my_str = 'one two three' print(my_str.rsplit(' ')) # ๐๏ธ ['one', 'two', 'three'] print(my_str.rsplit(' ', 1)) # ๐๏ธ ['one two', 'three']
The method takes the following 2 arguments:
Name | Description |
---|---|
separator | Split the string into substrings on each occurrence of the separator |
maxsplit | At most maxsplit splits are done, the rightmost ones (optional) |
Except for splitting from the right, rsplit()
behaves like split()
.
1
for the maxsplit
argument because we only want to split the string once, from the right.my_str = 'example.com/articles/python' result = my_str.rsplit('/', 1)[1] print(result) # ๐๏ธ 'python' # ๐๏ธ ['example.com/articles', 'python'] print(my_str.rsplit('/', 1))
The last step is to access the list element at index 1
to get a string
containing everything after the last occurrence of the specified character.
If you want to include the character in the result, use the addition (+) operator.
my_str = 'example.com/articles/python' result = '/' + my_str.rsplit('/', 1)[1] print(result) # ๐๏ธ '/python'
Alternatively, you can use the str.rpartition()
method.
my_str = 'example.com/articles/python' result = my_str.rpartition('/')[2] print(result) # ๐๏ธ 'python' # ๐๏ธ ('example.com/articles', '/', 'python') print(my_str.rpartition('/'))
The str.rpartition() method splits the string at the last occurrence of the provided separator.
The method returns a tuple containing 3 elements - the part before the separator, the separator, and the part after the separator.
If you need to include the separator in the result, use the str.join()
method
to join the second and third list items.
my_str = 'example.com/articles/python' result = ''.join(my_str.rpartition('/')[1:]) print(result) # ๐๏ธ '/python'
The str.join() method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.
The string the method is called on is used as the separator between the elements.
You can learn more about the related topics by checking out the following tutorials: