Codecademy Challenge - Python Unique Characters in a String

Hey guys, i am a bit stuck with the following challenge on Python:

Write a unique_characters() function that determines if any given string has all unique characters (i.e. no character in the string is duplicated). If the string has all unique characters, the function should return True. If the string does not have all unique characters, return False.

For example, unique_characters("apple") should return False.

I wrote my function in the following way, by using set():

def unique_characters(string_in):

if(len(set(string_in))==len(string_in)):
    return True 
else:
    return False 

However, when i run the piece of code, only 4/5 tests are passed.
Does anyone have any idea what could be the problem here?
Thank you :grin:

What am I missing? It works, right?

def unique_characters(string_in):
    if(len(set(string_in))==len(string_in)):
        return True 
    else:
        return False 

print(unique_characters("apple"))
print(unique_characters("Oconomowoc"))
print(unique_characters("Phil"))
print(unique_characters("Peter"))

False
False
True
False

you should also have the case when there is an empty string. In such a case, it does not return True/False because the string is empty hence you cannot consider if it has only unique characters

1 Like

I misunderstood the instructions. I thought it said that it couldn’t be empty.

1 Like