Convert string representation of Tuple to Tuple in Python

avatar

Borislav Hadzhiev

Last updated: Aug 30, 2022

banner

Photo from Unsplash

Convert string representation of Tuple to Tuple in Python #

Use the ast.literal_eval() method to convert the string representation of a tuple to a tuple, e.g. my_tuple = ast.literal_eval(my_str). The ast.literal_eval() method allows us to safely evaluate a string that contains a Python literal.

main.py
from ast import literal_eval my_str = '(1,2,3,4)' # ✅ convert string representation of tuple to tuple (ast.literal_eval()) my_tuple = literal_eval(my_str) print(my_tuple) # 👉️ (1, 2, 3, 4) print(type(my_tuple)) # 👉️ <class 'tuple'> # --------------------------------- # ✅ convert string representation of tuple to tuple (using split()) my_tuple = tuple(map(int, my_str.strip('()').split(','))) print(my_tuple) # 👉️ (1, 2, 3, 4) # --------------------------------- # 👇️ for comma-separated string my_str = "1,2,3,4" my_tuple = tuple(int(digit) for digit in my_str.split(",")) print(my_tuple) # 👉️ (1, 2, 3, 4)

The first example uses the ast.literal_eval() method to convert the string representation of a tuple to a tuple object.

main.py
from ast import literal_eval my_str = '(1,2,3,4)' my_tuple = literal_eval(my_str) print(my_tuple) # 👉️ (1, 2, 3, 4) print(type(my_tuple)) # 👉️ <class 'tuple'>

The ast.literal_eval method allows us to safely evaluate a string that contains a Python literal.

The string may consist of strings, bytes, numbers, tuples, lists, dicts, sets, booleans and None.

If you need to convert a comma-separated string to a tuple, use the str.split() method.

To convert a comma-separated string to a tuple:

  1. Use the str.split() method to split the string on each comma.
  2. If working with a numeric tuple, convert each item to an integer.
  3. Use the tuple() class to convert the generator object to a tuple.
main.py
my_str = "1,2,3,4" my_tuple = tuple(int(digit) for digit in my_str.split(",")) print(my_tuple) # 👉️ (1, 2, 3, 4)
If the elements in your string are separated with a comma followed by a space, make sure to adjust the argument to the split method to my_str.split(", ").

The first step is to split the string on each comma.

main.py
my_str = "1,2,3,4" print(my_str.split(',')) # 👉 ['1', '2', '3', '4'] my_str = "1, 2, 3, 4" print(my_str.split(', ')) # 👉️ ['1', '2', '3', '4']
If working with numeric tuples, use a generator expression to convert each string in the list to an integer and pass the generator object to the tuple() class.

If you have a tuple of strings, pass the call to the split() method to the tuple() class.

main.py
my_str = "a,b,c,d" my_tuple = tuple(my_str.split(",")) print(my_tuple) # 👉️ ('a', 'b', 'c', 'd')

If your string contains parentheses, use the str.strip() method to remove them before splitting on each comma.

main.py
my_str = '(1,2,3,4)' my_tuple = tuple(map(int, my_str.strip('()').split(','))) print(my_tuple) # 👉️ (1, 2, 3, 4)

The str.strip method returns a copy of the string with the specified leading and trailing characters removed.

We used the map() function to convert each item to an integer.

If you have a string containing quoted elements, use the str.replace() method to remove the unnecessary quotes.

main.py
my_str = "('a','b','c','d')" my_tuple = tuple(my_str.strip('()').replace("'", '').split(',')) print(my_tuple) # 👉️ ('a', 'b', 'c', 'd')

We used the str.replace() method to remove the single quotes from the string before calling the str.split() method.

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.