Python Practice › String Programs › Program 55

Find duplicate characters

EasyString Programs · 55 of 106

Write a Python program to find the characters that appear more than once (ignoring spaces).

Advertisement
Input
"hello world"
Output
lo

Approach

  1. Count characters and keep those with a count above 1.

Solution

Python
from collections import Counter text = "hello world"counts = Counter(text.replace(" ", ""))print("".join(ch for ch, n in counts.items() if n > 1))
Output
lo

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs

Leave a Comment