In Python, you can replace instances of a character in a string using the replace() method. This method takes two arguments: the character or substring to replace, and the replacement. Here’s an example:
text = “hello world”
new_text = text.replace(“o”, “0”)
print(new_text) # Output: hell0 w0rld
The replace() method returns a new string with the replacements made. If you only want to replace the first occurrence, you can pass a third argument to specify the maximum number of replacements:
text.replace(“o”, “0”, 1) # Replaces only the first “o”
This method does not modify the original string, as strings in Python are immutable.