Python string remove strings

Remove a part of a string (substring) in Python

This article explains how to remove a part of a string (= substring) in Python.

See the following article on how to remove extensions and directory parts from a path string.

If you want to remove part of the contents of a text file, read the file as a string, process it, and save it again.

Remove a substring by replacing it with an empty string

You can remove a substring by replacing it with an empty string » .

The examples below demonstrate basic usage of replace() and re.sub() . For a more in-depth guide on string replacement, refer to the following article:

Remove exact match string: replace()

You can replace a string that exactly matches the given string with the replace() method of the string str . If the match is replaced with an empty string » , the substring is effectively removed.

s = 'abc-xyz-123-789-ABC-XYZ' print(s.replace('xyz', '')) # abc--123-789-ABC-XYZ 

Remove substrings by regex: re.sub()

To remove substrings by regex, you can use sub() in the re module.

Читайте также:  HTML документ

The following example uses the regular expression pattern \d+ , which matches a sequence of one or more numbers. 123 and 789 are replaced by the empty string » and deleted.

import re s = 'abc-xyz-123-789-ABC-XYZ' print(re.sub('\d+', '', s)) # abc-xyz---ABC-XYZ 

Remove leading and/or trailing characters

To remove leading and/or trailing characters from a string, you can use strip() , lstrip() , rstrip() , removeprefix() , and removesuffix() .

Remove leading and trailing characters: strip()

Use strip() to remove specified characters at the leading and trailing of a string.

By default, consecutive whitespace characters at both ends are removed. Newlines \n , full-width spaces \u3000 , tabs \t , etc., are considered whitespace characters.

s = ' \n a b c \t' print(s) # # a b c print(repr(s)) # ' \n a b c\u3000\t' print(s.strip()) # a b c print(repr(s.strip())) # 'a b c' 

Here, the built-in repr() function is used to print whitespace characters.

strip() returns a new object while the original object remains unchanged. You can assign the result back to the original variable if desired. This behavior also applies to other string methods such as replace() , lstrip() , and rstrip() .

s_strip = s.strip() print(repr(s_strip)) # 'a b c' print(repr(s)) # ' \n a b c\u3000\t' s = s.strip() print(repr(s)) # 'a b c' 

When a string is passed to strip() , characters are removed from both ends.

The characters in the specified string are removed individually, not as a whole. For example, the result would be the same for ‘abc’ or ‘cba’ . If you want to remove the matched strings at both ends, use removeprefix() and removesuffix() as described below.

s = 'aabbcc-abc-aabbcc' print(s.strip('abc')) # -abc- print(s.strip('cba')) # -abc- print(s.strip('ab')) # cc-abc-aabbcc 

If a string is specified, whitespace characters are not removed.

s = ' \n aabbcc-abc-aabbcc \t' print(repr(s)) # ' \n aabbcc-abc-aabbcc\u3000\t' print(repr(s.strip('abc'))) # ' \n aabbcc-abc-aabbcc\u3000\t' 

If you want to remove whitespace characters in addition to the specified string, you need to either specify the whitespace characters explicitly or apply strip() multiple times.

print(repr(s.strip('abc \n \t'))) # '-abc-' print(repr(s.strip().strip('abc'))) # '-abc-' 

Remove leading characters: lstrip()

lstrip() removes only leading characters (left side) of a string. l is for left .

Usage is the same as strip() .

s = ' \n a b c \t' print(repr(s.lstrip())) # 'a b c \u3000\t' s = 'aabbcc-abc-aabbcc' print(s.lstrip('abc')) # -abc-aabbcc 

Remove trailing characters: rstrip()

rstrip() removes trailing characters (right side) of a string. r is for right .

Usage is the same as strip() .

s = ' \n a b c \t' print(repr(s.rstrip())) # ' \n a b c' s = 'aabbcc-abc-aabbcc' print(s.rstrip('abc')) # aabbcc-abc- 

Remove prefix: removeprefix() (Python 3.9 or later)

removeprefix() removes the specified prefix from a string. This method was added in Python 3.9.

If it starts with the specified prefix, a string with the prefix removed is returned. If there is no match, the original string is returned unchanged.

s = 'abc-abcxyz' print(s.removeprefix('abc-')) # abcxyz print(s.removeprefix('aabc-')) # abc-abcxyz 

Note that lstrip() removes all characters in the specified string from the left side of the input string.

Remove suffix: removesuffix() (Python 3.9 or later)

removesuffix() removes the specified suffix from a string. This method was added in Python 3.9.

The concept is the same as removeprefix() .

s = 'abcxyz-xyz' print(s.removesuffix('-xyz')) # abcxyz print(s.removesuffix('-xyzz')) # abcxyz-xyz 

To remove both the prefix and suffix, simply chain the removeprefix() and removesuffix() methods.

s = 'abc-abcxyz-xyz' print(s.removeprefix('abc-').removesuffix('-xyz')) # abcxyz 

Remove a substring by position and length: slicing

You can use slicing to get a part of a string at a given position.

s = '0123456789' print(s[3:7]) # 3456 print(s[3:-3]) # 3456 print(s[:5]) # 01234 print(s[5:]) # 56789 

If you want to delete both ends of a string, you can specify the part to be left using slices. For example, deleting the 6th character or later is equivalent to getting up to the 5th character.

To remove the inner string, slice parts to be left from both ends and concatenate them with the + operator.

For example, you may define the following functions.

Function to remove a substring from start to end (including end ):

def remove_str_start_end(s, start, end): return s[:start] + s[end + 1:] print(remove_str_start_end(s, 3, 5)) # 0126789 

Function to remove a substring of length characters from start .

def remove_str_start_length(s, start, length): return s[:start] + s[start + length:] print(remove_str_start_length(s, 3, 5)) # 01289 

Remove substrings from a list of strings

To remove substrings from a list of strings, you can use list comprehension to apply string methods such as strip() and slicing to each element.

l = ['Alice', 'Bob', 'Charlie'] print([s.strip('bce') for s in l]) # ['Ali', 'Bo', 'Charli'] print([s[:2] for s in l]) # ['Al', 'Bo', 'Ch'] 

Remove substrings from a string with line breaks

Use the following string with line breaks as an example.

s = 'Alice\nBob\nCharlie' print(s) # Alice # Bob # Charlie 

For more information on line breaks in Python, see the following article.

Remove a part of the string in each line

When removing a part of each line in a string containing newlines, you can use methods that operate on the entire string, such as replace() , without any special considerations.

print(s.replace('li', '')) # Ace # Bob # Chare 

On the other hand, methods like strip() act on the leading and trailing characters of the entire string, as demonstrated below.

print(s.strip('bce')) # Alice # Bob # Charli 

Slices are also processed for the entire string.

print(s[2:-2]) # ice # Bob # Charl 

To process each line individually, you should first split the lines using the splitlines() method.

l_s = s.splitlines() print(l_s) # ['Alice', 'Bob', 'Charlie'] 

Use list comprehension for this list.

l_s_strip = [line.strip('bce') for line in l_s] print(l_s_strip) # ['Ali', 'Bo', 'Charli'] 

Concatenate them into a single string with join() .

s_line_strip = '\n'.join(l_s_strip) print(s_line_strip) # Ali # Bo # Charli 

You can also combine the steps into a single operation. In the following example, a slice is applied to each line.

print('\n'.join([line[:2] for line in s.splitlines()])) # Al # Bo # Ch 

Remove lines according to condition

To remove lines that meet or do not meet a condition, add a condition to the list comprehension.

l_remove = [line for line in s.splitlines() if not line.startswith('B')] print(l_remove) # ['Alice', 'Charlie'] 

Once you have processed the lines, you can concatenate them back into a single string using the join() method.

s_line_remove = '\n'.join(l_remove) print(s_line_remove) # Alice # Charlie 

You can also combine these steps into a single expression.

print('\n'.join([line for line in s.splitlines() if 'li' in line])) # Alice # Charlie 

See the following article for more information on string conditions.

  • Split strings in Python (delimiter, line break, regex, etc.)
  • Count characters and strings in Python
  • Search for a string in Python (Check if a substring is included/Get a substring position)
  • Extract a substring from a string in Python (position, regex)
  • Replace strings in Python (replace, translate, re.sub, re.subn)
  • String comparison in Python (exact/partial match, etc.)
  • Regular expressions with the re module in Python
  • How to use regex match objects in Python
  • Handle line breaks (newlines) in strings in Python
  • Raw strings in Python
  • Convert Unicode code point and character to each other (chr, ord)
  • Extract and replace elements that meet the conditions of a list of strings in Python
  • How to use print() in Python
  • Check if a string is numeric, alphabetic, alphanumeric, or ASCII
  • Convert a list of strings and a list of numbers to each other in Python

Источник

Remove Substring From a String in Python

In this tutorial, we will look at how to remove a substring from a string in Python with the help of some examples.

How to remove a substring from a string?

Remove substring from a string

You can use the string replace() function to remove a substring from a string in Python. Use the replace() function to replace the substring with an empty string. Let’s look at an example –

📚 Discover Online Data Science Courses & Programs (Enroll for Free)

Introductory ⭐

Intermediate ⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# create a string s = "Wingardium. Leviosa. " # remove substring s.replace(". ", '')

Here, we removed the substring “…” from the string “Wingardium…Leviosa…”.

Note the that string replace() function will replace every occurrence of the substring in the string.

Remove substring from the beginning of a string

If you specifically want to remove a substring only from the beginning of a string, slice the string starting from the length of the substring.

# create a string s = "Capt. Rogers and Capt. Bakshi" # store the substring to be removed from the beginning sub = "Capt." # remove substring s[len(sub):]

The substring is removed from the front. Since we only want to remove the substring from the beginning, we use slicing to only keep the part of the string that comes after the substring.

Also, note that this method will not work if the substring is not present at the beginning of the string. This is because we are slicing the string irrespective of whether the substring is present or not in the string.

Remove substring from the end of a string

Similarly, if you want to remove a substring only from the end of a string, slice the string from its beginning to the starting index of the substring. Using a negative index can be helpful here.

# create a string s = "one mississippi two mississippi" # store the substring to be removed from the end sub = "mississippi" # remove substring s[:-len(sub)]

The resulting string doesn’t have the substring in the end.

Again, note that this method will not work if the substring is not present at the end of the string. This is because we are slicing the string irrespective of whether the substring is present or not in the string.

You might also be interested in –

Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.

Author

Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects. View all posts

Data Science Parichay is an educational website offering easy-to-understand tutorials on topics in Data Science with the help of clear and fun examples.

Источник

Оцените статью