Python Challenge - Unique Characters in a String

def unique_characters(string_in): length = len(string_in) unique = len(set(string_in)) if length == 0: print('error') elif length == unique: return True else: return False print(unique_characters("apple"))
def unique_characters(string_in):
  if len(string_in) > 0:
    x = set(string_in)
  else:
    return "Error"
  
  if len(x) == len(string_in):
    return True
  else:
    return False

def unique_characters(string_in):
word_set = set(string_in)
if string_in:
if len(word_set)==len(string_in):
return True
else:
return False
else:
return ‘Error,string is empty’
print(unique_characters(“apple”))
print(unique_characters(“”))

def unique_characters(string_in):
ch = set(string_in)
if(string_in == “”):
return “String is empty”
elif (len(ch) == len(string_in)):
return True
return False

print(unique_characters(“apple”))

def unique_characters(string_in):
    """ I used the set() function to create a set of unique characters in the input string.
        Then, I compared the length of the set to the length of the input string to check if all characters in the string are 
        unique
        If the lengths are equal, it means that all characters in the string are unique. If not, it means that there are duplicate 
        characters in the string
        The if 'string_in' checks if the 'string_in' parameter is a non-empty string"""
    return len(set(string_in)) == len(string_in) if string_in else "error"
   
print(unique_characters("apple"))
1 Like
def unique_characters(string_in): print(f'The Boolean value of "{string_in} word has only unique characters" is: \n') for idx in range(len(string_in)): for target_value in string_in[idx+1:]: if string_in[idx] == target_value: return False if string_in: return True #Function call string_in = unique_characters('apple') print(string_in)

[codebyte]
Beginner here, I got it figured out but yours seems much more simple, I learned the set() function by investigating your solution
[python/codebyte]
def unique_characters(string_in):
chars =
for char in string_in:
chars.append(char)
x = chars.count(char)
if x > 1:
return False
if len(string_in) < 1:
return “Error”
return True

print(unique_characters(“apple”))

I dont even know how to post a bit of code… sorry for the messy response

To preserve code formatting in forum posts, see: [How to] Format code in posts

To post a Codebyte, see: NEW Feature: Codebytes

Usually, properly formatted code (first link) is easier to read and scroll as compared to a Codebyte.

def unique_characters(string_in): if(string_in == ""): return "There is an error" while len(string_in) > 1: first = string_in[0] string_in = string_in[1:] if (first in string_in): return False return True

My Solution:

def unique_characters(string_in): if string_in=="": return "Error" else: ans=True for ch in string_in: if string_in.count(ch)>1: ans=False break return ans print(unique_characters("apple"))

Here is my answer

def unique_characters(string_in): if len(string_in) == 0: return "Sorry, no empty strings please!" char_dict = {} for char in string_in: if char in char_dict: char_dict[char] += 1 else: char_dict[char] = 1 for key, val in char_dict.items(): if val > 1: return False return True print(unique_characters("apple"))

Here is my solution:

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

print(unique_characters("apple"))

Because since sets don’t allow duplicate items, we can check the length to see if any duplicated item has been removed. Enjoy!

def unique_characters(string_in): #Check if string input is empty, if so return error if len(string_in) == 0: return 'Error' #reassign string input as all lowercase character string_in = string_in.lower() #define empty list of unique characters unique_char_list = [] #loop through all characters in string_in for char in string_in: #if character is not in unique char list we will append list with that character if char not in unique_char_list: unique_char_list.append(char) else: #if character is in out unique character list, return false return False #if we loop through all characters and all are identified as unique we return True return True print(unique_characters(""))

Here is my submission. Any feedback appreciated.

My solution without using loops. The use of built-in functions len and set.
If the string is empty, return the message “String is empty” otherwise compare the length of the string with the length of the unique characters, i.e. len(set(string_in))

def unique_characters(string_in):
  if len(string_in) == 0:
    return "String is empty"
  return len(string_in) == len(set(string_in))

print(unique_characters("apple"))