• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / Python / Replace Character in String in Python

Replace Character in String in Python

August 19, 2022 Leave a Comment

To replace a character in a string with another character in Python, you can use the string replace() function.

example_string = "This is a string."

string_with_i_replaced_with_e = example_string .replace("i","e")

print(string_with_i_replaced_with_e)

#Output:
Thes es a streng.

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.

We can use the replace() function to replace a specific character in a string with another character.

To replace the i’s of a string with e’s, for example, we can use the replace() function in the following Python code.

example_string = "This is a string."

string_with_i_replaced_with_e = example_string .replace("i","e")

print(string_with_i_replaced_with_e)

#Output:
Thes es a streng.

If you wanted to remove all occurrences of a character from a string, you can use a replacement value of an empty string.

example_string = "This is a string."

string_with_i_removed = example_string .replace("i","")

print(string_with_i_removed)

#Output:
Ths s a strng.

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

Below are a few more examples of how you can use the replace() function to make replacements in strings in Python.

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.

You can also replace full words with other words. Let’s replace the word “a” with “the”.

string_with_spaces = "This is a string."

string_with_the = string_with_spaces.replace("a","the")

print(string_with_the)

#Output:
This is the string.

Hopefully this article has been useful for you to learn how to replace specific characters with other characters in a string in Python.

Other Articles You'll Also Like:

  • 1.  Open Multiple Files Using with open in Python
  • 2.  How to Sort Numbers in Python Without Sort Function
  • 3.  Check if Class is Subclass in Python with issubclass()
  • 4.  Using Python to Insert Tab in String
  • 5.  Get Day of the Year with Python
  • 6.  Get Month from Datetime in pandas DataFrame
  • 7.  Selenium minimize_window() Function to Minimize Window in Python
  • 8.  pandas variance – Compute Variance of Variables in DataFrame
  • 9.  pandas ewm – Calculate Exponentially Weighted Statistics in DataFrame
  • 10.  Python Factorial Recursion – Using Recursive Function to Find Factorials

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 *

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