Convert a String to a Set in Python

avatar

Borislav Hadzhiev

Last updated: Sep 2, 2022

banner

Photo from Unsplash

Convert a String to a Set in Python #

Use the set() class to convert a string to a set, e.g. my_set = set(my_str). The set() class will convert the string to a set by splitting its characters.

main.py
my_str = 'one' # ✅ Convert string to set by splitting characters my_set = set(my_str) print(my_set) # 👉️ {'n', 'o', 'e'} # ---------------------------------------------------- # ✅ Convert string to set without splitting characters my_set = set([my_str]) print(my_set) # 👉️ {'one'} # ---------------------------------------------------- # ✅ Convert string to set with multiple elements my_str = 'one,two,three' my_set = set(my_str.split(',')) print(my_set) # 👉️ {'one', 'two', 'three'} # ---------------------------------------------------- # ✅ Convert string to set with multiple integer elements my_str = '1,2,3' my_set = set(int(item) for item in my_str.split(',')) print(my_set) # 👉️ {1, 2, 3}

The first example uses the set() class to convert a string to a set object by splitting its characters.

main.py
my_str = 'one' my_set = set(my_str) print(my_set) # 👉️ {'n', 'o', 'e'}

Each character in the string becomes a separate set element.

You can use the same approach if you need to convert a string to a set of integers.

main.py
my_str = '123' my_set = set(int(digit) for digit in my_str) print(my_set) # 👉️ {1, 2, 3}

We used a generator expression to iterate over the string.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we convert the current digit to an integer and return the result.

Alternatively, you can pass a list to the set() class.

To convert a string to a set without splitting the string's characters, pass a list containing the string to the set() class, e.g. my_set = set([my_str]). The set will contain the string as its single element.

main.py
my_str = 'one' my_set = set([my_str]) print(my_set) # 👉️ {'one'}

The set() class takes an iterable optional argument and returns a new set object with elements taken from the iterable.

main.py
empty_set = set() print(empty_set) # 👉️ set() my_set = set(['one', 'two', 'three']) print(my_set) # 👉️ {'three', 'two', 'one'}

If you need to split a string on a delimiter to create a set object, use the str.split() method.

main.py
my_str = 'one,two,three' my_set = set(my_str.split(',')) print(my_set) # 👉️ {'one', 'two', 'three'}

We used the str.split() method to split the string on each comma and passed the result to the set() class to create a set object.

main.py
my_str = 'one,two,three' print(my_str.split(',')) # 👉️ ['one', 'two', 'three']

The str.split() method splits the string into a list of substrings using a delimiter.

The method takes the following 2 parameters:

NameDescription
separatorSplit the string into substrings on each occurrence of the separator
maxsplitAt most maxsplit splits are done (optional)
When no separator is passed to the str.split() method, it splits the input string on one or more whitespace characters.

If the separator is not found in the string, a list containing only 1 element is returned.

You can use a generator expression if you need to split a string into a set containing integer elements.

main.py
my_str = '1,2,3' my_set = set(int(item) for item in my_str.split(',')) print(my_set) # 👉️ {1, 2, 3}

We used a generator expression to iterate over the list and used the int() class to convert each item to an integer.

The set object contains integer elements.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.