Concatenated string with uncommon characters in Python?

In Python, Strings are one of the commonly used data types that are used to store sequences of characters. In this article, we are going to learn to concatenate strings with uncommon characters in Python.

The uncommon characters are the characters that appear in one string but not in the other string. For example, if str1="ABC" and str2="BCD", the uncommon characters are 'A' and 'D', and the concatenated result is "AD".

Using set.symmetric_difference() Method

The set.symmetric_difference() method returns a new set that contains elements present in either of the sets, but not in both. This makes it perfect for finding uncommon characters ?

Syntax

set.symmetric_difference(other)

Example

In the following example, we find uncommon characters between two strings and concatenate them in sorted order ?

def find_uncommon_chars(x, y):
    set_x = set(x)
    set_y = set(y)
    uncommon = set_x.symmetric_difference(set_y)
    return ''.join(sorted(uncommon))

# Test the function
result = find_uncommon_chars("Hello", "Hi")
print("Uncommon characters:", result)

The output of the above program is as follows ?

Uncommon characters: ello

Using Python Loops

We can manually check for uncommon characters by iterating through both strings and collecting characters that don't appear in the other string ?

Example

In the following example, we use loops to find and concatenate uncommon characters ?

def find_uncommon_with_loops(str1, str2):
    result = ''
    
    # Find characters in str1 but not in str2
    for char in str1:
        if char not in str2 and char not in result:
            result += char
    
    # Find characters in str2 but not in str1        
    for char in str2:
        if char not in str1 and char not in result:
            result += char
            
    return result

# Test the function
result = find_uncommon_with_loops("ciaz", "cruze")
print("Uncommon characters:", result)

The output of the above program is as follows ?

Uncommon characters: iaue

Using List Comprehension

We can also use list comprehension for a more concise approach to find uncommon characters ?

Example

def find_uncommon_list_comp(str1, str2):
    # Characters in str1 but not in str2
    uncommon1 = [char for char in str1 if char not in str2]
    # Characters in str2 but not in str1
    uncommon2 = [char for char in str2 if char not in str1]
    
    # Remove duplicates using set and join
    all_uncommon = list(set(uncommon1 + uncommon2))
    return ''.join(sorted(all_uncommon))

# Test the function
result = find_uncommon_list_comp("python", "java")
print("Uncommon characters:", result)
Uncommon characters: ajhnoptyv

Comparison

Method Readability Performance Best For
symmetric_difference() High Fast Clean, efficient solution
Loops Medium Slow Understanding the logic
List Comprehension High Medium Pythonic approach

Conclusion

Use set.symmetric_difference() for the most efficient way to find uncommon characters between strings. The loop method helps understand the underlying logic, while list comprehension offers a Pythonic middle ground.

Updated on: 2026-03-24T21:02:03+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements