• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Remove Substring from String in Python with replace()

Remove Substring from String in Python with replace()

August 24, 2022 Leave a Comment

To remove a specific substring in a string variable in Python, the easiest way is to use the Python string replace() function.

s = "This is a string."

string_without_substring = s.replace("is","")

print(string_without_substring)

#Output:
This  a string.

When working with strings in Python, being able to manipulate your variables easily is important. There are a number of built in string methods which allow us to get information and change string variables.

One such function which is very useful is the string replace() function. With the replace() function, we can create a new string where the specified value is replaced by another specified value.

replace() takes a string to search for and a replacement value and replacing all found strings with the replacement value.

To remove a given substring from a string, you can use replace() and pass the substring as the search value and an empty string as the replacement value as shown below.

s = "This is a string."

string_without_substring = s.replace("is","")

print(string_without_substring)

#Output:
This  a string.

Using the replace() function to Make Replacements in Strings in Python

You can use replace() for many other cases in your Python programs.

For example, if we want to replace spaces with dashes, we can do the following.

string_with_spaces = "This is a string."

string_with_dashes = string_with_spaces.replace(" ","-")

print(string_with_dashes)

#Output:
This-is-a-string.

If we want to replace all the spaces with periods, we can do so easily in the following Python code.

string_with_spaces = "This is a string."

string_with_periods = string_with_spaces.replace(" ","-")

print(string_with_periods)

#Output:
This.is.a.string.

Hopefully this article has been useful for you to learn how to remove a substring from a string in Python.

Other Articles You'll Also Like:

  • 1.  pandas str replace – Replace Text in Dataframe with Regex Patterns
  • 2.  Using Python to Find Maximum Value in List
  • 3.  Python Get First Word in String
  • 4.  PROC REG Equivalent in Python
  • 5.  Python Trig – Using Trigonometric Functions in Python for Trigonometry
  • 6.  Convert pandas Series to Dictionary in Python
  • 7.  Get Week Number from Date in Python
  • 8.  Using Python to Calculate Average of List of Numbers
  • 9.  Python not in – Check if Value is Not Included in Object
  • 10.  pandas floor – Find the Floor of a Column Using Numpy floor

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy