5

Remove String before a Specific Character in Python

 2 years ago
source link: https://thispointer.com/remove-string-before-a-specific-character-in-python/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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"
"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"
"Warrior"

There are different ways to do this. Let’s discuss them one by one.

Advertisements

vid5e6258f9da92c874459691.jpg?cbuster=1600267117
00:00/11:46
liveView.php?hash=ozcmPTEznXRiPTEzqzyxX2V2ZW50PTUjJaNypaZypyRcoWU9MTY0OTY0OTE3MSZ2nWRspGkurWVlVzVlPTMhMS4jJaM9MTAkMwx3JaN0YT0jJat9NDUmJax9MmI1JaZcZF9jYXNmRG9gYWyhPXRbnXNjo2yhqGVlLzNioSZmqWJJZD10nGympG9coaRypv5wo20zZGVvqWqJozZipz1uqGyiow1BQyQyMwAyMxYyMwBSVEJcpHY2JTIjJTJGJTIjMSUlMCUlRvUlMGRyZzF1oHQznXNBpHA9MCZlnT02QmY5NmY2NTUmNmQ2MTp0NmM3QmpmNxImMTqCNTQmMDqEN0I2NDMlMmAmMwMlMxQmMDM0MxQmMTMkNUYmMDM2N0Q3QwpmMmEmMwMmMmQmOTM2MmQmOTqEN0I0MmMkMmpmMwqEN0I1MmY0NDp2ODpjNwMmMmQlNmY2MTU3MmUmMDVBNTt0OTp1NTxmMwM5NmQ3RDqCNwI2MmY4NmI2RwZENwU3RDqCNmE2NDY1NmM2Qwp0NxY3MDqEN0I2RwZDNwx2RTp1Nmt3RDqCNTtmNDM1MmM3RDqCNTxmMmMlMmU3RDqCNwYmMTqEN0I3MwUkNTU0QTU1NDx0MmM4Nwp1NTZDNTI0MmYkNTt0MwMlNEU2OTQkNmY0OTQ0NDU2NmRDNmx0MwZCNUE1NmVBNwt2NDU3NmtmMDqEN0I0Qm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tyMwB4ODZsNwQyMwxyMwBBpHBfZVqyYxgcqCUlRwUmNl4mNvUlMCUlOEgIVE1MJTJDJTIjoGyeZSUlMEqyY2giJTI5JTIjQ2ulo21yJTJGNmphMC4mODY1LwElMCUlMFNuZzFlnSUlRwUmNl4mNvZwp3V1nWQ9NwI1M2E2MTIlZzNyNCZ2SW1jT3Bjo3J0qW5cqHyNqWk0nXBfnWVlPTUzY29hqGVhqEZcoGVJZD0jJz1yZGyuUGkurUkcp3RJZD0jJz1yZGyuTGymqEyxPTAzZ2Rjpw0jJzqxpHJDo25mZW50PSZcp1qyUGFmp0qxpHI9MSZwY3BuPTAzY2NjYUNioaNyoaQ9JzNvqXN0ZXI9MTY0OTY0OTE3ODpmMCZ1nWQ9U2VenW5xo1NQoGF5ZXI2MwUmYTYkMmp2OWE0JaB1YyVloD1bqHRjplUmQSUlRvUlRaRbnXNjo2yhqGVlLzNioSUlRaJyoW92ZS1mqHJcozpgYzVzo3JyLWEgp3ByY2yznWMgY2uupzFwqGVlLWyhLXB5qGuiovUlRvZzoG9uqFN0YXR1pm1zYWkmZSZynWRmpD1jpzVvnWQ=

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,

  • 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)
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
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)
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
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)
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)
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
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 = '-'
# Remove all characters before the character '-' from string
strValue = strValue[strValue.index(ch) + 1 : ]
except ValueError:
print(strValue)
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
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.

Pandas Tutorials -Learn Data Analysis with Python

 

 

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK