Python String – Replace() Method

Replace string python: A string is a character sequence.
A character is nothing more than a symbol. The English language, for example, has 26 characters.
Computers do not work with characters, but rather with numbers (binary). Despite the very fact that you simply see characters on your screen, they’re 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.
A string in Python may be 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.

String Replace() Function

1)replace() function

replace() is a built-in Python function that returns a copy of the string in which all occurrences of a substring are replaced with another substring.

Syntax:

given_string.replace(old, new, count)

Parameters:

  • old−This is an old substring that must get replaced .
  • new − this is often a replacement substring which will replace the old one.
  • max − Only the primary count occurrences are replaced if the optional argument max is provided.

Return:
This method returns a string copy during which all occurrences of substring old are replaced with new. Only the primary count occurrences are replaced if the optional argument max is provided.

2)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 replaceString(string, oldstring, replacestring):
    # using replace
    resultstring = string.replace(oldstring, replacestring)

    # return the final string
    return resultstring


# Driver code
# given string
string = "This is BTechGeeks new online learning platform"
# string which needs to be replaced
oldstring = 'e'
# replacing string/new string
replacestring = 't'
# passing these strings to replaceString function
print(replaceString(string, oldstring, replacestring))

Output:

This is BTtchGttks ntw onlint ltarning platform

3)Replace only first two occurences of string:

Instead of replacing all occurrences of a character in a string, we can use the replace() function’s count argument to replace only the first few occurrences of a character.

Below is the implementation:

# 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 = '$$$'
# passing these strings to replaceMultipleString function
print(replaceMultipleString(string, oldstring, replacestring))

Output:

H$$$llo this is BT$$$chGeeks

4)Replace the first occurrence of a given substring in a string with another character

If we only want to replace the first occurrences of a substring in a string with another character or substring, we must use the replace() function with the count argument set to 1.

Below is the implementation:

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

    # return the final string
    return resultstring


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

Output:

Hello tht is BTechGeeks

Related Programs: