Python Replace Last Two Characters in String | In this article, we will learn how to replace the last two characters in a string in python. We will see three methods by which we can find replace the last two digits in a string, along with a number of examples. Let’s get started!
Python Replace Last Two Characters in String Using .replace() function
To substitute strings in a distinct result, we can apply the str data type’s replace() method. The replace() method takes two parameters:- the regex function you want to use to match strings, and the substitute string for the found strings.
The replace() has another optional parameter that accepts a number to specify the maximum number of replacements to execute. The replace() method would only find and replace two instances inside the text if the count parameter is set to 2.
txt = "Know Program" print("String: ", txt) size = len(txt) rep = "XY" txt = txt.replace(txt[size - 2:], rep) print("Replaced string: ", txt)
String: Know Program
Replaced string: Know ProgrXY
Let us see the above program by taking input from the end-user.
str = input("Enter the string: ") size = len(str) rep = "XY" str = str.replace(str[size - 2:], rep) print("Replaced string: ", str)
Enter the string: Programming
Replaced string: ProgrammiXY
Python Replace Last Two Characters in String using regex function, re.sub()
You must import the re module in Python, which contains a number of expressions corresponding operations for regex that you can use. There are three primary arguments to the function. The first argument is a regex pattern, the second is a string to swap the found sequences, and the third argument is the string to act on. Make a function that converts all the integers in a string to the letters XY.
import re def replace(str, strToReplace): str = re.sub('am', strToReplace, str) print(str) str = "Know Program" strToReplace = "XY" replace(str, strToReplace)
Python Replace Last Two Characters in String Using re.subn() function
This method is similar to re.sub(), rather than returning the converted text, it returns a tuple with the count of substitutions attempted. The parsed string is assigned to txt, and the replaced letters are allocated to t.
import re str = "Python is the best language." def replace(str): str, t = re.subn('ge', 'XY', str) print(str) replace(str)
Python is the best languaXY.
Note: The programs will return the same input or string if the specified letters with which it is supposed to be replaced are not present in the string or input.
For example, our input is “hello world” and the code says “replace xy with ab”, we will get “hello world” in the output.
This brings us to the end of the article where we saw how to replace the last two characters in a string in python using three methods. Also see:- How to Print the Square of a Number in Python
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!