Write a Python program to Convert String to Set


The program is a demonstration of how you can convert a string into a set in Python. The initial string s is assigned the value "TutorJoes". The type of the string is then printed which is "str". Then, the built-in set() function is used to convert the string into a set. The newly created set is stored in the variable new. The elements of the set are unordered and have no duplicates, which are the unique characters of the string. Finally, the value and type of the newly created set new are printed, which shows that the string has been successfully converted into a set.

Source Code

# create a string str
s = "TutorJoes"
print("String  Value : " ,s)
print("Type of string : " ,type(s))
 
# convert String to Set
new = set(s)
print("\nString  Convert to Set : ", new)
print("Type of String : ",type(new))
 

Output

String  Value :  TutorJoes
Type of string :  <class 'str'>

String  Convert to Set :  {'t', 'r', 'o', 'e', 's', 'u', 'J', 'T'}
Type of String :  <class 'set'>