Python Practice › String Programs › Program 78
Keep only one given character
Write a Python program to remove all characters except a given one.
Input
"Sqatools python", keep "S"
Output
S
Approach
- Keep characters equal to the given one.
Solution
text = "Sqatools python"keep = "S"print("".join(ch for ch in text if ch == keep))Output
S
Learn the concept
New to this topic? Read the lesson, then come back.