Python Practice › String Programs › Program 17

Replace Java with Python, any case

EasyString Programs · 17 of 106

Write a Python program to replace the word “Java” (in any letter case) with “Python”.

Advertisement
Input
"JAVA is the Best Programming Language in the Market"
Output
Python is the Best Programming Language in the Market

Approach

  1. str.replace is case-sensitive, so use re.sub with re.IGNORECASE.

Solution

Python
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.

More string programs

← All 106 String Programs

Leave a Comment