Python Practice › String Programs › Program 17
Replace Java with Python, any case
Write a Python program to replace the word “Java” (in any letter case) with “Python”.
Input
"JAVA is the Best Programming Language in the Market"
Output
Python is the Best Programming Language in the Market
Approach
str.replaceis case-sensitive, so usere.subwithre.IGNORECASE.
Solution
import re text = "JAVA is the Best Programming Language in the Market"print(re.sub(r"\bjava\b", "Python", text, flags=re.IGNORECASE))Output
Python is the Best Programming Language in the Market
Line by line
Line 4
\b matches whole words only, and the flag ignores case.
Learn the concept
New to this topic? Read the lesson, then come back.