A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/convert-string-to-set-in-python/ below:

Convert String to Set in Python

Convert String to Set in Python

Last Updated : 15 Jul, 2025

There are multiple ways of converting a String to a Set in python, here are some of the methods.

Using set()

The easiest way of converting a string to a set is by using the set() function.

Example 1 : 

Python
s = "Geeks"

print(type(s))
print(s)

# Convert String to Set
set_s = set(s)

print(type(set_s))
print(set_s)

Output
<class 'str'>
Geeks
<class 'set'>
{'s', 'e', 'k', 'G'}

Let's explore other methods of converting string to set:

Using set() with split() method (for words)

To convert words from a sentence into a set, use spilt() method before set().

Example:

Python
s = "Geek for Geeks"

# split the string and then convert it to set
set_words = set(s.split())
print(set_words)

Output
{'for', 'Geeks', 'Geek'}
Using dict.fromkeys()

We can also convert a string to a set by using dict.fromkeys() as it creates a dictionary from an iterable and sets the values to none for every key.

Example:

Python
# create a string
s = "developer"

set_s = set(dict.fromkeys(s))
print(set_s)

Output
{'l', 'v', 'o', 'd', 'e', 'p', 'r'}


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4