Python Practice › String Programs › Program 78

Keep only one given character

EasyString Programs · 78 of 106

Write a Python program to remove all characters except a given one.

Advertisement
Input
"Sqatools python", keep "S"
Output
S

Approach

  1. Keep characters equal to the given one.

Solution

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

More string programs

← All 106 String Programs

Leave a Comment