How do you remove part of a text in python?

If you need to strip some end of a string if it exists otherwise do nothing. My best solutions. You probably will want to use one of first 2 implementations however I have included the 3rd for completeness.

For a constant suffix:

def remove_suffix(v, s):
    return v[:-len(s)] if v.endswith(s) else v
remove_suffix("abc.com", ".com") == 'abc'
remove_suffix("abc", ".com") == 'abc'

For a regex:

def remove_suffix_compile(suffix_pattern):
    r = re.compile(f"(.*?)({suffix_pattern})?$")
    return lambda v: r.match(v)[1]
remove_domain = remove_suffix_compile(r"\.[a-zA-Z0-9]{3,}")
remove_domain("abc.com") == "abc"
remove_domain("sub.abc.net") == "sub.abc"
remove_domain("abc.") == "abc."
remove_domain("abc") == "abc"

For a collection of constant suffixes the asymptotically fastest way for a large number of calls:

def remove_suffix_preprocess(*suffixes):
    suffixes = set(suffixes)
    try:
        suffixes.remove('')
    except KeyError:
        pass

    def helper(suffixes, pos):
        if len(suffixes) == 1:
            suf = suffixes[0]
            l = -len(suf)
            ls = slice(0, l)
            return lambda v: v[ls] if v.endswith(suf) else v
        si = iter(suffixes)
        ml = len(next(si))
        exact = False
        for suf in si:
            l = len(suf)
            if -l == pos:
                exact = True
            else:
                ml = min(len(suf), ml)
        ml = -ml
        suffix_dict = {}
        for suf in suffixes:
            sub = suf[ml:pos]
            if sub in suffix_dict:
                suffix_dict[sub].append(suf)
            else:
                suffix_dict[sub] = [suf]
        if exact:
            del suffix_dict['']
            for key in suffix_dict:
                suffix_dict[key] = helper([s[:pos] for s in suffix_dict[key]], None)
            return lambda v: suffix_dict.get(v[ml:pos], lambda v: v)(v[:pos])
        else:
            for key in suffix_dict:
                suffix_dict[key] = helper(suffix_dict[key], ml)
            return lambda v: suffix_dict.get(v[ml:pos], lambda v: v)(v)
    return helper(tuple(suffixes), None)
domain_remove = remove_suffix_preprocess(".com", ".net", ".edu", ".uk", '.tv', '.co.uk', '.org.uk')

the final one is probably significantly faster in pypy then cpython. The regex variant is likely faster than this for virtually all cases that do not involve huge dictionaries of potential suffixes that cannot be easily represented as a regex at least in cPython.

In PyPy the regex variant is almost certainly slower for large number of calls or long strings even if the re module uses a DFA compiling regex engine as the vast majority of the overhead of the lambda's will be optimized out by the JIT.

In cPython however the fact that your running c code for the regex compare almost certainly outweighs the algorithmic advantages of the suffix collection version in almost all cases.

Edit: https://m.xkcd.com/859/

  1. HowTo
  2. Python How-To's
  3. Remove Substring From String in Python

Created: November-01, 2020 | Updated: December-10, 2020

  1. Use str.replace() Method to Replace Substring From String in Python 3.x
  2. Use string.replace() Method to Replace Substring From String in Python 2.x
  3. Use str.removesuffix() to Remove Suffix From String

This tutorial describes how to remove a substring from a string in Python. It will tell us that strings can not be just removed but just replaced. The tutorial also lists some example codes to clarify the concepts as the method has changed from previous Python versions.

Use str.replace() Method to Replace Substring From String in Python 3.x

There are many built-in methods available for strings. Actually, strings are immutable in Python. You can usestr.replace() method to create a new string. str.replace(oldvalue, newvalue, count) returns a copy of string whose oldvalue is replaced with newvalue. count tells how many occurrences the replacement will be performed.

list_str = {'Abc.ex', 'Bcd.ex', 'cde.ex', 'def.jpg', 'efg.jpg'}
new_set = {x.replace('.ex', '').replace('.jpg', '') for x in list_str}
print(new_set)

Output:

{'Bcd', 'Abc', 'cde', 'def', 'efg'}

Use string.replace() Method to Replace Substring From String in Python 2.x

If you are using Python 2.x, you can use string.replace() method to replace a substring. This method takes old value, new value, and count as its parameters. new value is required to replace the old value and count is a number specifying how many occurrences of the old value you want to replace. Default is all occurrences.

An example code for this method is given below:

text = "Hello World!"
x = text.replace("l", "k", 1)
print(x)

Output:

Heklo World!

Use str.removesuffix() to Remove Suffix From String

If you are using Python 3.9, you could remove suffix using str.removesuffix('suffix').

If the string ends with a suffix string and the suffix is non-empty, return the string with suffix removed. Otherwise, the original string will be returned.

The basis example for str.removesuffix() is given below:

text = 'Quickly'
print(text.removesuffix('ly'))
print(text.removesuffix('World'))

Output:

Quick
Quickly

Related Article - Python String

  • Remove Commas From String in Python
  • Check a String Is Empty in a Pythonic Way
  • Convert a String to Variable Name in Python
  • Remove Whitespace From a String in Python
  • How do you remove part of a text in python?

    How do you trim text in Python?

    Python Trim String.
    strip(): returns a new string after removing any leading and trailing whitespaces including tabs ( \t )..
    rstrip(): returns a new string with trailing whitespace removed. ... .
    lstrip(): returns a new string with leading whitespace removed, or removing whitespaces from the “left” side of the string..

    How do I remove part of a string?

    We can remove part of the string using REPLACE() function. We can use this function if we know the exact character of the string to remove. REMOVE(): This function replaces all occurrences of a substring within a new substring.