Python String replace() Method

The string replace() method in Python is a built-in string method used to replace all occurrences of a substring within a string with another substring.


Syntax:
string.replace(old_substring, new_substring, count)
  1. string: the original string to be modified
  2. old_substring: the substring to be replaced
  3. new_substring: the substring to replace old_substring
  4. count (optional): the number of occurrences of old_substring to be replaced.

Following are some examples of the replace() method in action:


Replace a single occurrence of a substring within a string
string = "Hello, World!" new_string = string.replace("World", "Python") print(new_string) # Output: "Hello, Python!"

In the above example, the replace() method replaces the first occurrence of "World" with "Python" in the string.


Replace all occurrences of a substring within a string
string = "the quick brown fox jumps over the lazy dog" new_string = string.replace("the", "a") print(new_string) # Output: "a quick brown fox jumps over a lazy dog"

In the above example, the replace() method replaces all occurrences of "the" with "a" in the string.


Limit the number of occurrences to replace using the count parameter
string = "the quick brown fox jumps over the lazy dog" new_string = string.replace("the", "a", 1) print(new_string) # Output: "a quick brown fox jumps over the lazy dog"

In the above example, the replace() method replaces the first occurrences of "the" with "a" in the string.


Replace a single character within a string
string = "Hello, World!" new_string = string.replace("o", "0") print(new_string) # Output: "Hell0, W0rld!"

In the above example, the replace() method replaces all occurrences of "o" with "0" in the string.


Replace a substring with an empty string to remove it from the original string
string = "Hello, World!" new_string = string.replace(", ", "") print(new_string) # Output: "HelloWorld!"

In the above example, the replace() method replaces the substring ", " (a comma followed by a space) with an empty string, effectively removing it from the original string.

String in Python

In Python, a string is a sequence of characters enclosed in either single quotes ('...') or double quotes ("..."). Python treats both single quotes and double quotes the same way, so you can use either type to define a string. If you need to use a quote inside a string that's enclosed in the same type of quotes, you can use the other type of quotes to define the string.


how to replace a string in python

Strings are immutable in Python, which means that you cannot change individual characters within a string. However, you can create a new string by concatenating two or more strings using the + operator. You can also access individual characters within a string using indexing.

Commonly used string methods

Following are some commonly used string methods in Python:

  1. str.upper(): Returns a copy of the string with all the characters converted to uppercase.
  2. str.lower(): Returns a copy of the string with all the characters converted to lowercase.
  3. str.strip(): Returns a copy of the string with leading and trailing whitespace removed.
  4. str.isdigit(): Returns True if all the characters in the string are digits, and False otherwise.
  5. str.isalnum(): Returns True if all the characters in the string are alphanumeric (letters or digits), and False otherwise.
  6. str.isalpha(): Returns True if all the characters in the string are alphabetic, and False otherwise.
  7. str.islower(): Returns True if all the cased characters in the string are lowercase, and there is at least one cased character in the string, and False otherwise.