Concatenated string with uncommon characters in Python Last Updated : 14 Jan, 2025 Comments Improve Suggest changes Like Article Like Report The goal is to combine two strings and identify the characters that appear in one string but not the other. These uncommon characters are then joined together in a specific order. In this article, we'll explore various methods to solve this problem using Python.Using set symmetric difference We can use the symmetric difference operation of the set to pull out all the uncommon characters from both the string and make a string. Python s1 = 'aacdb' s2 = 'gafd' # Find and join uncommon characters print(''.join(set(s1) ^ set(s2))) Outputfbgc Explanation:set(s1) and set(2): This converts the string s1 and s2 into a set of unique characters. This removes any duplicates.^ (Symmetric Difference): This operator performs a symmetric difference between two sets. It returns all elements that are in either set(s1) or set(s2), but not in both.''.join(): ThisJoins the resulting set of characters back into a string without spaces.Let's understand different methods to concatenated string with uncommon characters .Using collections.Countercollections.Counter count the occurrences of each character in the combined strings, then filters out characters that appear more than once. Python from collections import Counter s1 = 'aacdb' s2 = 'gafd' f = Counter(s1 + s2) # Filter and collect characters that appear only once res = [ch for ch in s1 + s2 if f[ch] == 1] print(''.join(res)) Outputcbgf Explanation:Counter(str1 + str2): This counts all characters in the combined string str1 + str2.List comprehension: This Filters out characters that appear more than once.''.join(result): This combines the filtered characters into a string.Using DictionaryThis method uses a dictionary to manually count the frequency of characters in the combined strings, then filters out those that appear only once. Python s1 = 'aacdb' s2 = 'gafd' # Initialize an empty dictionary f = {} for ch in s1 + s2: f[ch] = f.get(ch, 0) + 1 # Filter characters that appear only once res = [ch for ch in s1 + s2 if f[ch] == 1] print(''.join(res)) Outputcbgf Explanation:Frequency dictionary: It is used to count the occurrences of each character in the combined strings s1 + s2.f[ch] == 1): This filters out characters that appear only once.''.join(res): This joins the filtered characters into a final string.Using two pass filteringThis method identifies common characters between two strings and then filters them out in separate passes. It is simple but less efficient due to the extra overhead of processing the strings twice. Python s1 = 'aacdb' s2 = 'gafd' c = set(s1) & set(s2) # Filter out common characters in two passes res = ''.join([ch for ch in s1 if ch not in c] + [ch for ch in s2 if ch not in c]) print(res) Outputcbgf Explanation:set(s1) & set(s2): This finds the intersection of s1 and s2, i.e., the common characters.Two-pass filtering: This filters the characters in both strings by checking if they are not in the common set. Comment More infoAdvertise with us Next Article Concatenated string with uncommon characters in Python S Shashank Mishra Follow Improve Article Tags : Strings Python DSA python-list python-set python-string Python list-programs Python string-programs Python set-programs +5 More Practice Tags : pythonpython-listpython-setStrings Similar Reads Convert Hex To String Without 0X in Python Hexadecimal representation is a common format for expressing binary data in a human-readable form. In Python, converting hexadecimal values to strings is a frequent task, and developers often seek efficient and clean approaches. In this article, we'll explore three different methods to convert hex t 2 min read How to Convert Bytes to String in Python ? We are given data in bytes format and our task is to convert it into a readable string. This is common when dealing with files, network responses, or binary data. For example, if the input is b'hello', the output will be 'hello'.This article covers different ways to convert bytes into strings in Pyt 2 min read Replacing Characters in a String Using Dictionary in Python In Python, we can replace characters in a string dynamically based on a dictionary. Each key in the dictionary represents the character to be replaced, and its value specifies the replacement. For example, given the string "hello world" and a dictionary {'h': 'H', 'o': 'O'}, the output would be "Hel 2 min read Python String Concatenation String concatenation in Python allows us to combine two or more strings into one. In this article, we will explore various methods for achieving this. The most simple way to concatenate strings in Python is by using the + operator.Using + OperatorUsing + operator allows us to concatenation or join s 3 min read Convert a String to Utf-8 in Python Unicode Transformation Format 8 (UTF-8) is a widely used character encoding that represents each character in a string using variable-length byte sequences. In Python, converting a string to UTF-8 is a common task, and there are several simple methods to achieve this. In this article, we will explor 3 min read Ways to Print Escape Characters in Python In Python, escape characters like \n (newline) and \t (tab) are used for formatting, with \n moving text to a new line and \t adding a tab space. By default, Python interprets these sequences, so I\nLove\tPython will display "Love" on a new line and a tab before "Python." However, if you want to dis 2 min read How To Print Unicode Character In Python? Unicode characters play a crucial role in handling diverse text and symbols in Python programming. This article will guide you through the process of printing Unicode characters in Python, showcasing five simple and effective methods to enhance your ability to work with a wide range of characters Pr 2 min read Convert Unicode String to a Byte String in Python Python is a versatile programming language known for its simplicity and readability. Unicode support is a crucial aspect of Python, allowing developers to handle characters from various scripts and languages. However, there are instances where you might need to convert a Unicode string to a regular 2 min read Convert Unicode to Bytes in Python Unicode, often known as the Universal Character Set, is a standard for text encoding. The primary objective of Unicode is to create a universal character set that can represent text in any language or writing system. Text characters from various writing systems are given distinctive representations 2 min read Convert Hex to String in Python Hexadecimal (base-16) is a compact way of representing binary data using digits 0-9 and letters A-F. It's commonly used in encoding, networking, cryptography and low-level programming. In Python, converting hex to string is straightforward and useful for processing encoded data.Using List Comprehens 2 min read Like