How to Replace First Occurrence in Python String?
Hi Dev,
In this short tutorial we will cover an how to replace first occurrence in python string. In this article, we will implement a python replace first occurrence in string. This post will give you simple example of how to replace first occurrence of string in python. if you have question about how to replace only first occurrence of string in python then I will give simple example with solution. you will do the following things for python replace first occurrence in string
In this examples, i will add myString variable with hello string. Then we will use replace() function to replace first 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 Specific Word in String
replaceString = myString.replace("This", "That", 1)
print(replaceString)
Output:
Hello, That is nicesnippets.com, This is awesome.
- Get Last 2 Digits of Number in Python Tutorial Example
- How to Sum of First and Last Digit of Number in Python?
- How to Get First and Last Digit of Number in Python?
- Get the Last Digit of Number in Python Tutorial Example
- Get the First Digit of Number in Python Tutorial Example
- How to Remove All Decimals from Number in Python?
- How to Convert String to Float with 2 Decimal Places in Python?
- How to Format Number to 2 Decimal Places in Python?