How to Replace First Character Occurrence in Python String?

15-Mar-2023

.

Admin

How to Replace First Character Occurrence in Python String?

Hi Dev,

Today our leading topic is how to replace first character occurrence in python string. I would like to show you python replace first character occurrence in string. This article will give you simple example of how to replace first character occurrence of string in python. This tutorial will give you simple example of how to replace only first character occurrence of string in python.

In this example, i will add myString variable with hello string. Then we will use replace() function to replace first character occurrence from string in python. So, without further ado, let's see simple examples: You can use these examples with python3 (Python 3) version.

Example


main.py

# Declare String Variable

myString = "Hello, This is nicesnippets.com This is awesome."

# Python Replace First Character

replaceString = myString.replace("T", "X", 1)

print(replaceString)

Output:

Hello, Xhis is nicesnippets.com This is awesome.

#Python