'Doctest letter extraction

Extract all the unique letters present in the text string. :param text: string data :return: a tuple of all the unique letters of the string in lowercase

 def extract_letters(text):
    """
        Extract all the unique letters present in the text string.
        :param text: string data
        :return: a tuple of all the unique letters of the string in lowercase
        >>> extract_letters("Python 123 is C00!_")
        ('p', 'y', 't', 'h', 'o', 'n', 'i', 's', 'c')
        >>> extract_letters("Numbers Are Overrated!")
        ('n', 'u', 'm', 'b', 'e', 'r', 's', 'a', 'o', 'v', 't', 'd')
    """
    letters = []
    users = []
    
    for i in range(len(text)):
        if (text[i]).isalpha():
            letters.append(text[i].lower())
    
    for i in range(len(letters)):
        if letters[i] not in users:
            users.append(letters[i])
    
    return (users)

Was wondering if there was a way to make the output curved brackets instead of square brackets so it matches and the doctest completes?



Solution 1:[1]

Try this:

def extract_letters(text):
    """
        Extract all the unique letters present in the text string.
        :param text: string data
        :return: a tuple of all the unique letters of the string in lowercase
        >>> extract_letters("Python 123 is C00!_")
        ('p', 'y', 't', 'h', 'o', 'n', 'i', 's', 'c')
        >>> extract_letters("Numbers Are Overrated!")
        ('n', 'u', 'm', 'b', 'e', 'r', 's', 'a', 'o', 'v', 't', 'd')
    """
    letters = []
    users = []
    
    for i in range(len(text)):
        if (text[i]).isalpha():
            letters.append(text[i].lower())
    
    for i in range(len(letters)):
        if letters[i] not in users:
            users.append(letters[i])
    
    return tuple((users)) #returns result converted from list to tuple.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bialomazur