Python: use string replace function - how it works
Related Videos: Python Replace String Method (May 2024).
The string replace method is very practical in Python, with which you can easily exchange substrings. This practical tip shows you how to use the function.
Python: use string replace function - how it works
The string replace function is already integrated in Python and does not have to be imported by you.
- First of all you have to store a string with a variable. For example, "string1 =" This is a sample string "".
- The syntax of this method is "string1.replace (old, new [, max])". The old substring to be replaced is represented by "old". The new string is stored at "new". You can use the "max" argument to specify the maximum number of replacements.
- An example would be the command "print (string1.replace (" This ", " That "))". As a result, you would now get "This is a sample string".
Tip: You can find more programming tutorials on our Python programming language topic page. There we show you, for example, how you can use substrings in Python.