62.9k views
2 votes
Write a function that takes in a string as input and returns a list of the unique characters used in the string. Punctuation should not be counted as a unique character. For our purposes you may consider the following characters as punctuation: .,;:?!"'- Your list should return all letters as lower case and sorted. So for example if the input is ' Happy days are here again!' your function should return the list

User Kalik
by
4.8k points

1 Answer

7 votes

Answer:

def texted():

text= input("Enter Text: ")

text = text.lower()

nlist = ' '

for char in text:

if char.isalnum() == True:

if char not in nlist:

nlist = nlist + char

nlist = sorted(nlist)

print(nlist)

return;

texted()

Step-by-step explanation:

Step 1:

Define the function, i.e give it a name.

def texted():

Step 2:

Prompt the user to input a string i.e Text

text= input("Enter Text: ")

Step 3:

use the lower() string method. it is a method in python used to convert uppercase string characters to lower case.

text = text.lower()

Step 4:

Declare an empty string variable that will be used to store the characters while sorting.

nlist = ' '

Step 5:

for char in text:

if char.isalnum() == True:

if char not in nlist:

nlist = nlist + char

-Use a for loop to iterate through all the characters in the text.

-.isalnum() is a method in python that returns true if a character is an alphabet or a number.

The first if statement checks if it is an alphabet or a number and if it passes as true, it passes that alphabet or number to the next if statement.

- The second if statement checks if the character is not already in the list, if it is in the list it would not add it a second time.

If the character is not in the list, it adds the character to the list.

Step 6:

- sorts the list, i.e arranges the list in an alphabetical order.

- prints out the list, i.e produces an output of the list to your computer screen.

nlist = sorted(nlist)

print(nlist)

  • texted() this is used to call the function.
Write a function that takes in a string as input and returns a list of the unique-example-1
User Petercopter
by
5.0k points