Convert Python Strings into Tuple



Converting Python String into Tuple

We can convert a Python string into tuples by simply mentioning a comma (,) after the string. This will treat the string as a single element to the tuple.

Example

Here our string variable "s" is treated as one item in the tuple, which can be done by adding the comma after the string -

s = "python"
print("Input string :", s)

t = s,
print('Output tuple:', t)
print(type(t))

Following is the output of the above code -

Input string : python
Output tuple: ('python',)
<class 'tuple'>

Using tuple() Function

Also, we can use the tuple() function to convert the given string into a tuple. The tuple() is a Python built-in function that is used to create a tuple from an iterable.

Example

Here the tuple() function assumes that each character of the string represents an individual item -

s = "python"
print("Input string :", s)

result = tuple(s)
print('Output tuple:', result)

Following is the output of the above code -

Input string : python
Output tuple: ('p', 'y', 't', 'h', 'o', 'n')

Using string.split() Method

If the input string has space-separated characters and we want only those characters, then we can use the string.split() method to avoid calculating whitespace as an element.

The split() method splits the given data into different sections based on the default delimiter (white space " ") or specified delimiter. It returns a list of string elements that are separated based on the specified delimiter.

Example

In the following example, the string with space-separated characters is split and then converted to a tuple successfully by using the string. split() and tuple() functions -

s = "a b c d e"
print("Input string :", s)

result = tuple(s.split())
print('Output tuple:', result)

Following is the output of the above code -

Input string : a b c d e
Output tuple: ('a', 'b', 'c', 'd', 'e')

Example

In the following example, the elements of the string are separated by "@" character, and we have separated the elements by specifying s.split("@") to separate the elements of the string, and then it is converted to tuple -

s = "a@b@c@d@e"
print("input string :", s)

result = tuple(s.split("@"))
print('Output tuple:', result)

Following is the output of the above code -

input string : a@b@c@d@e
Output tuple: ('a', 'b', 'c', 'd', 'e')

Using map() and int() Functions

If the given string has numeric values represented in string format, then the converted tuple elements are also represented in string format only. If we want to convert the type tuple elements, then we need to use the map() and int() functions together.

  • map(): The map function is used to apply the given function to every element of the iterable.

  • Int(): The int() function returns a converted integer object from the given string/number.

Example

In the following example, we have converted the given string into a tuple using the map() and int() functions -

s = "1 2 3 4 5"
print("Input string :", s)

result = tuple(map(int, s.split(" ")))
print('Output tuple:', result)
print("Type of tuple element: ", type(result[1]))

Following is the output of the above code -

Input string : 1 2 3 4 5
Output tuple: (1, 2, 3, 4, 5)
Type of tuple element:  <class 'int'>
Updated on: 2025-05-29T11:17:09+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started