Python Practice › String Programs › Program 77

Uncommon characters of two strings

EasyString Programs · 77 of 106

Write a Python program to build a string from the characters that appear in only one of two strings.

Advertisement
Input
"abcdefg", "xyzabcd"
Output
efgxyz

Approach

  1. Keep characters of each string that are not in the other.

Solution

Python
s1, s2 = "abcdefg", "xyzabcd"result = [ch for ch in s1 if ch not in s2] + [ch for ch in s2 if ch not in s1]print("".join(result))
Output
efgxyz

Learn the concept

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

More string programs

← All 106 String Programs

Leave a Comment