Skip to main content

How to Split Strings and Convert into Tuples in Python

This guide explores various techniques for splitting strings and creating tuples in Python. We will cover converting comma-separated strings to tuples, handling string representations of tuples, and creating tuples from a combination of strings and lists.

Splitting a String into a Tuple

To split a delimited string into a tuple:

  1. Use str.split() with the delimiter to create a list of substrings.
  2. Use tuple() to convert the resulting list to a tuple.
my_str = 'tutorial,reference,com'
my_tuple = tuple(my_str.split(','))
print(my_tuple) # Output: ('tutorial', 'reference', 'com')
  • The split(',') splits the string at each comma.

1. Splitting Strings on Whitespace

If no delimiter is provided to str.split(), it splits on whitespace:

my_str = 'tutorial reference com'
my_tuple = tuple(my_str.split())
print(my_tuple) # Output: ('tutorial', 'reference', 'com')

2. Excluding Leading/Trailing Delimiters

If your string might have leading or trailing delimiters, you can use a generator expression to filter the empty strings that might result.

my_str = ',tutorial,reference,com,'
my_tuple = tuple(item for item in my_str.split(',') if item)
print(my_tuple) # Output: ('tutorial', 'reference', 'com')
  • The generator expression iterates over the splitted string and only returns non-empty strings.

Converting Comma-Separated Strings to Integer Tuples

To create a tuple of integers from a comma-separated string:

my_str = '1,2,3,4,5'
my_tuple = tuple(int(item) for item in my_str.split(','))
print(my_tuple) # Output: (1, 2, 3, 4, 5)
  • The list comprehension converts each split substring to an integer using int().
  • The tuple constructor then converts the list of integers into a tuple.

Converting String Representations of Tuples to Tuples

Using ast.literal_eval()

The safest way to convert a string representation of a tuple to a tuple object is using ast.literal_eval():

from ast import literal_eval

my_str = '(1,2,3,4)'
my_tuple = literal_eval(my_str)
print(my_tuple) # Output: (1, 2, 3, 4)
print(type(my_tuple)) # Output: <class 'tuple'>
  • literal_eval() safely evaluates strings only if they represent valid Python datatypes.

Handling Quoted Elements

If the elements are quoted in the string you can strip the quotes using str.strip() or str.replace():

my_str = "('a','b','c','d')"
my_tuple = tuple(my_str.strip('()').replace("'", '').split(','))
print(my_tuple) # Output: ('a', 'b', 'c', 'd')

Using map()

To convert the elements into specific types, such as integers, use the map function:

my_str = '(1,2,3,4)'
my_tuple = tuple(map(int, my_str.strip('()').split(',')))
print(my_tuple) # Output: (1, 2, 3, 4)
  • my_str.strip('()') removes parentheses.
  • The split(',') method creates a list of individual string numbers.
  • The map() function applies int() to each substring, converting it to an integer value.

Converting a String to a Tuple without Splitting

To create a tuple containing a single string, use a trailing comma:

my_str = 'one'
my_tuple = (my_str,)
print(my_tuple) # Output: ('one',)
print(type(my_tuple)) # Output: <class 'tuple'>
  • The trailing comma distinguishes a tuple from a string enclosed in parentheses.

Creating Tuples from Strings and Lists

There are two approaches to create a tuple from a combination of strings and lists: concatenation and list merging.

Using Tuple Concatenation

Use tuple concatenation with trailing comma and tuple() to combine a string and list into a new tuple:

my_str = 'tutorial'
my_list = ['reference', 'com']
my_tuple = (my_str,) + tuple(my_list)
print(my_tuple) # Output: ('tutorial', 'reference', 'com')

  • (my_str,) creates a tuple from the string.
  • tuple(my_list) creates a tuple from the list.
  • + concatenates them.

Using List Concatenation and Tuple Conversion

Merge the string and the list using the + operator with list, and convert the result into a tuple using the tuple() constructor:

my_str = 'tutorial'
my_list = ['reference', 'com']

my_tuple = tuple([my_str] + my_list)
print(my_tuple) # Output: ('tutorial', 'reference', 'com')
  • [my_str] wraps the string in a list, enabling list merging.