Remove String after a Specific Character in Python

This article will discuss different ways to remove all characters from a string after a specific character in Python.

Table Of Contents

Suppose we have a string,

"The Last-Warrior"

Now, we want to delete all characters after the character ‘-‘ from this string, including character ‘-‘. The final string must be like,

"The Last"

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

Remove everything after 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, it 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 after 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 1 to the original string variable. It will give an effect that we have deleted everything after the character ‘-‘ in a string.

For example,

strValue = "The Last-Warrior"

ch = '-'

# Remove all characters after the character '-' from string
strValue = strValue.split(ch, 1)[0]

print(strValue)

Output:

The Last

It deleted everything after the character ‘-‘ from the string.

Remove everything after 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 separator occurrence and return a 3-tuple containing the part before the separator, the separator, and the part after the separator.

To remove everything after the first occurrence of the character ‘-‘ in a string, pass the character ‘-‘ as a separator in the partition() function. Then assign the part before the separator to the original string variable. It will affect that we have deleted everything after the character ‘-‘ in a string.

For example,

strValue = "The Last-Warrior"

ch = '-'

# Remove all characters after the character '-' from string
before, sep, after = strValue.partition('-')

strValue = before

print(strValue)

Output:

The Last

It deleted everything after the character ‘-‘ from the string.

Remove everything after 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 after the first occurrence of the character ‘-‘ in a string. We need 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 after '-'
pattern  = ch + ".*"

# Remove all characters after the character '-' from string
strValue = re.sub(pattern, '', strValue )

print(strValue)

Output:

The Last

It deleted everything after the character ‘-‘ from the string.

Remove everything after a character in a string using subscript operator

Search for the index position of the first occurrence of the character ‘-‘ in the string and select substring from the start till that index position. 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 after the character '-' from string
    strValue = strValue[0 : strValue.index(ch)]
except ValueError:
    pass

print(strValue)

Output:

The Last

It deleted everything after the character ‘-‘ from the string.

Summary

We learned how to delete everything from a string after a specific character in Python.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top