This article will discuss different ways to remove all characters before a specific character from a string in Python.
Table Of Contents
Suppose we have a string,
"The Last-Warrior"
We want to delete all characters before the character ‘-‘ from this string, including the character ‘-‘ itself. The final string must be like,
"Warrior"
There are different ways to do this. Let’s discuss them one by one.
Remove everything before a character in a string using split()
In Python, the string class provides a function split(). It accepts two arguments i.e. separator and max split value. Based on the separator, splits the string into different parts. The maximum limit of these parts can be specified as the second argument of split() function.
To remove everything before the first occurrence of the character ‘-‘ in a string, pass the character ‘-‘ as separator and 1 as the max split value. The split(‘-‘, 1) function will split the string into 2 parts,
Frequently Asked:
- Remove duplicate spaces from string in Python
- How to Get a SubString from a String in Python?
- Â Convert Bytes to a String in Python
- Count occurrences of a single or multiple characters in string and find their index positions
- Part 1 should contain all characters before the first occurrence of character ‘-‘.
- Part 2 should contain all characters after the first occurrence of character ‘-‘.
Then assign part 2 to the original string variable. It will give an effect that we have deleted everything before the character ‘-‘ in a string.
For example,
strValue = "The Last-Warrior" ch = '-' # Remove all characters before the character '-' from string listOfWords = strValue.split(ch, 1) if len(listOfWords) > 0: strValue = listOfWords[1] print(strValue)
Output:
Warrior
It deleted everything before the character ‘-‘ from the string.
Remove everything before a character in a string using partition()
In Python, the string class provides a function partition(sep). It accepts a separator as an argument and splits the string into three parts based on the given separator. It will look for the first occurrence of the seperator and returns a 3-tuple containing the part before the separator, the separator, and the part before the separator.
To remove everything before the first occurrence of the character ‘-‘ in a string, pass the character ‘-‘ as a separator in the partition() function. Then assign the part after the separator to the original string variable. It will give an effect that we have deleted everything before the character ‘-‘ in a string.
For example,
strValue = "The Last-Warrior" ch = '-' # Remove all characters before the character '-' from string before, sep, after = strValue.partition('-') if len(after) > 0: strValue = after print(strValue)
Output:
Warrior
It deleted everything before the character ‘-‘ from the string.
Remove everything before a character in a string using Regex
In Python, the regex module provides a function to replace the contents of a string based on a matching regex pattern. The signature of function is like this,
sub(pattern, replacement_str, original_str)
It looks for the matches of the given regex pattern in the sting original_str and replaces all occurrences of matches with the string replacement_str.
We can use this to remove everything before the first occurrence of the character ‘-‘ in a string. We need to use the “.*-” as a regex pattern and an empty string as the replacement string.
For example,
import re strValue = "The Last-Warrior" ch = '-' # The Regex pattern to match al characters on and before '-' pattern = ".*" + ch # Remove all characters before the character '-' from string strValue = re.sub(pattern, '', strValue ) print(strValue)
Output:
Warrior
It deleted everything before the character ‘-‘ from the string.
Remove everything before a character in a string using the subscript operator
Search for the index position of the first occurrence of the character ‘-‘ in the string and select substring from this index position till the end of string. If the character ‘-‘ doesn’t exist in the string, then it will raise a ValueError.
For example,
strValue = "The Last-Warrior" ch = '-' try: # Remove all characters before the character '-' from string strValue = strValue[strValue.index(ch) + 1 : ] except ValueError: pass print(strValue)
Output:
Warrior
It deleted everything before the character ‘-‘ from the string.
Summary
We learned how to delete everything from a string before a specific character in Python.