To convert a string to a tuple in Python, wrap the result of the conversion you need in tuple(). Use tuple(text.split()) for a tuple of words, tuple(text.split(",")) for delimited fields, and tuple(text) for a tuple of single characters. If the string already contains tuple syntax such as "(1, 2, 3)", parse it with ast.literal_eval().
A tuple is immutable and hashable when its elements are, which makes it a good result type for fixed records and dictionary keys.
tuple() takes any iterable. A string iterates over its characters, so passing the string directly gives one item per character.
Be careful not to confuse this with putting the whole string in a one-element tuple. That needs a trailing comma:
Split first, then convert the resulting list. split() returns a list of strings, and tuple() freezes it.
With no argument split() splits on runs of whitespace. With an explicit separator, whitespace around fields remains, so strip it when needed:
Pass a generator expression to tuple() to transform, filter or convert types in the same step. It avoids building an intermediate list.
int() raises ValueError if a field is not a valid integer, so wrap the conversion in try and except when reading untrusted input.
If the text is "(1, 2, 3)", tuple() would split it into characters including the brackets. Parse it as a Python literal instead:
ast.literal_eval() accepts only literals and does not run code, so it is safe where eval() is not. Note that "1, 2, 3" without brackets also evaluates to a tuple, and a single value such as "5" evaluates to the integer 5, not a tuple, so check the type of the result.
| Input string | Wanted result | Code |
|---|---|---|
"Hello" | characters | tuple(text) |
"a b c" | words | tuple(text.split()) |
"a,b,c" | fields | tuple(text.split(",")) |
"1 2 3" | integers | tuple(map(int, text.split())) |
"(1, 2, 3)" | parsed tuple | ast.literal_eval(text) |
"hello" | one-element tuple | (text,) |
A tuple built from a string works well for unpacking and as a dictionary key:
Unpacking raises ValueError if the number of items does not match the number of variables. Use name, *rest = ... when the length varies.
Real strings are often messy. Decide what should happen with empty strings, extra separators and wrong field counts before you convert.
For real CSV data that can contain quoted commas, use the csv module rather than split(","), then call tuple(row) on each parsed row.
Put it in a tuple display with a trailing comma: (text,). Calling tuple(text) always iterates over the characters.
tuple(text) and tuple(text.split())?tuple(text) gives single characters, including spaces. tuple(text.split()) gives whole words.
"(1, 2, 3)" into a tuple of numbers?Use ast.literal_eval("(1, 2, 3)"). It parses the literal and returns (1, 2, 3) with integers.
No. Convert it to a list with list(), change the list, then convert back with tuple().
Use tuple(text) for characters, tuple(text.split(...)) for words and fields, and a generator expression when you need to convert or clean the items. Use ast.literal_eval() for strings that already contain tuple syntax, and add a trailing comma when you want a one-element tuple holding the whole string.