Convert a comma-separated string to a Tuple in Python

avatar

Borislav Hadzhiev

Last updated: Aug 31, 2022

banner

Photo from Unsplash

Convert a comma-separated string to a Tuple in Python #

To convert a comma-separated string to a tuple:

  1. Use the str.split() method to split the string on each comma.
  2. Use the tuple() class to convert the list of strings to a tuple.
main.py
my_str = 'one,two,three,four,five' # ✅ convert comma-separated string to tuple my_tuple = tuple(my_str.split(',')) print(my_tuple) # 👉️ ('one', 'two', 'three', 'four', 'five') # ---------------------------------- my_str = '1,2,3,4,5' # ✅ convert comma-separated string to tuple of integers my_tuple = tuple(int(item) if item.isdigit() else item for item in my_str.split(',')) print(my_tuple) # 👉️ (1, 2, 3, 4, 5) # ---------------------------------- # 👇️ If your string has leading or trailing commas my_str = ',one,two,three,four,five,' my_tuple = tuple(item for item in my_str.split(',') if item) print(my_tuple) # 👉️ ('one', 'two', 'three', 'four', 'five')

The first example converts a comma-separated string to a tuple with string elements.

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

main.py
my_str = 'one,two,three,four,five' # 👇️ ['one', 'two', 'three', 'four', 'five'] print(my_str.split(',')) my_tuple = tuple(my_str.split(',')) print(my_tuple) # 👉️ ('one', 'two', 'three', 'four', 'five')

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)
If the separator is not found in the string, a list containing only 1 element is returned.

The last step is to use the tuple() class to convert the list to a tuple.

The tuple class takes an iterable and returns a tuple object.

To convert a comma-separated string to a tuple of integers:

  1. Use the str.split() method to split the string on each comma.
  2. Convert each item in the list to an integer.
  3. Use the tuple() class to convert the list to a tuple.
main.py
my_str = '1,2,3,4,5' my_tuple = tuple(int(item) if item.isdigit() else item for item in my_str.split(',')) print(my_tuple) # 👉️ (1, 2, 3, 4, 5)

We split the string on each comma and used a generator expression to iterate over the list.

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 check if the current item is a digit and use the int() class to convert the matching elements.

The str.isdigit method returns True if all characters in the string are digits and there is at least 1 character, otherwise False is returned.

If you have a guarantee that the string only contains comma-separated integers, you can skip the call to the str.isdigit() method.

main.py
my_str = '1,2,3,4,5' my_tuple = tuple(int(item) for item in my_str.split(',')) print(my_tuple) # 👉️ (1, 2, 3, 4, 5)

If your string contains leading or trailing commas, use a generator expression to exclude the empty string elements.

main.py
my_str = ',one,two,three,four,five,' my_tuple = tuple(item for item in my_str.split(',') if item) print(my_tuple) # 👉️ ('one', 'two', 'three', 'four', 'five')

The string in the example has a leading and trailing comma, so splitting on a comma returns empty string elements.

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

You can use a generator expression to exclude the empty strings from the result.

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.