Exercise 85

Write a python script that determines the set of all characters that make up a string s. Example if s = "Python language", the algorithm returns the set:

{'y', 'h', 'a', 'e', 'P', 'g', 'o', 't', 'l', 'n', 'u', ' '}

Solution

We will give an algorithm as a function called find_unique_characters() which takes a string s as input and returns a set containing all unique characters present in the string.

def find_unique_characters(s):
    # Create an empty set to store unique characters
    unique_chars = set()
    
    # Iterate through each character in the string
    for char in s:
        # Add the character to the set
        unique_chars.add(char)
    
    # Return the set of unique characters
    return unique_chars

# Example usage:
s = "Python language"
result = find_unique_characters(s)
print(result)
One thought on “Solution Exercicse 85 : Python set of all characters that make up a string s”

Leave a Reply