How to Replace Single or Multiple Characters in a String

Python : How to Replace Single or Multiple Characters in a String ?

A string is a series of characters.

A character is nothing more than a representation of something. For example, the English language has 26 characters.
Computers do not work with characters, but rather with numbers (binary). Despite the fact that you see characters on your screen, they are internally stored and manipulated as a series of 0s and 1s.
Encoding is the process of converting a character to a number, and decoding is the reverse process. ASCII and Unicode are two of the most common encodings.
In Python, a string is a sequence of Unicode characters.. Unicode was created to include every character in all languages and to bring encoding uniformity. Python Unicode can teach you everything you need to know about Unicode.

Examples:

Input:

string="Hello this is BTechGeeks" oldstring='e' replacestring='q'

Output:

Hqllo this is BTqchGqqks

Replace Single or Multiple Characters in a String

There are several ways to replace single or multiple characters in a String of them are:

Python has a string.replace() function

Syntax:

string.replace(old, new , count)

It returns a new string object that is a copy of the existing string with the content replaced. In addition, if count is not provided, it will return a string with all occurrences of ‘old’ replaced with ‘new’ string.
If the count parameter is supplied, it will return a string with the first ‘count’ occurrences of the ‘old’ string replaced with the ‘new’ string.

Replace all instances of a character or string in a string:

Assume we have a string, and now replace all occurrences of ‘e’ with ‘q’.

Below is the implementation:

# Function which replaces the string
def replaceMultipleString(string, oldstring, replacestring):
    # using replace
    resultstring = string.replace(oldstring, replacestring)

    # return the final string
    return resultstring


# Driver code
# given string
string = "Hello this is BTechGeeks"
# string which needs to be replaced
oldstring = 'e'
# replacing string/new string
replacestring = 'q'
# passing these strings to replaceMultipleString function
print(replaceMultipleString(string, oldstring, replacestring))

Output:

Hqllo this is BTqchGqqks

Because strings are immutable in Python, we can’t change their contents. As a result, member functions such as replace() produce a new string.

Replace the first n occurrences of a given character / substring in a string with another character / substring.

Suppose we want to replace character ‘e’ with ‘q000’ for first 2 i.e n occurrences then,

# Function which replaces the string
def replaceMultipleString(string, oldstring, replacestring):
    # using replace
    # providing count =2
    resultstring = string.replace(oldstring, replacestring, 2)

    # return the final string
    return resultstring


# Driver code
# given string
string = "Hello this is BTechGeeks"
# string which needs to be replaced
oldstring = 'e'
# replacing string/new string
replacestring = 'q000'
# passing these strings to replaceMultipleString function
print(replaceMultipleString(string, oldstring, replacestring))

Output:

Hq000llo this is BTq000chGeeks

Replace multiple strings/characters in a string

The string.replace() function can only replace occurrences of a single substring.

But what if we need to replace multiple substrings within the same string?

To replace all occurrences of these three characters ‘e’, ‘h’, and ‘i’ with the string ‘##’ let us write a new function that extends replace().

Below is the implementation:

# Function which replaces the string
def replaceMultipleString(string, oldstring, replacestring):
  # Traverse the old string which needs to be replaced
    for element in oldstring:
        # Check if string is in the original string
        if element in string:
            # Replace the string eith replacestring
            string = string.replace(element, replacestring)

    # return the final string
    return string


# Driver code
# given string
string = "Hello this is BTechGeeks"
# string which needs to be replaced
oldstring = ['e', 'i', 'h']
# replacing string/new string
replacestring = '##'
# passing these strings to replaceMultipleString function
print(replaceMultipleString(string, oldstring, replacestring))

Output:

H##llo t####s ##s BT##c##G####ks

 
Related Programs: