Open In App

# Python | Convert String to Tuple

Interconversion of data types is very common problem one can face while programming. There can be a problem in which we need to convert a string of integers to a tuple. Let’s discuss certain ways in which this can be done.

Method #1 : Using map() + int + split() + tuple()

This method can be used to solve this particular task. In this, we just split each element of string and convert to list and then we convert the list to resultant tuple.

## Python3

 `# Python3 code to demonstrate working of``# Convert String to Tuple``# using map() + tuple() + int + split()` `# initialize string``test_str ``=` `"``1``, ``-``5``, ``4``, ``6``, ``7``"` `# printing original string``print``("The original string : " ``+` `str``(test_str))` `# Convert String to Tuple``# using map() + tuple() + int + split()``res ``=` `tuple``(``map``(``int``, test_str.split(``', '``)))` `# printing result``print``("``Tuple` `after getting conversion ``from` `String : " ``+` `str``(res))`

Output :

```The original string : 1, -5, 4, 6, 7
Tuple after getting conversion from String : (1, -5, 4, 6, 7)```

Time complexity: O(n), where n is the length of the input string. This is because the program splits the input string and maps each element to an integer using the map() function, which requires iterating through each element of the string once.
Auxiliary space: O(n), where n is the length of the input string. This is because the program creates a tuple to store the resulting integers, which can have a maximum length of n. Therefore, the program uses O(n) additional memory to store its data structures.

Method #2: Using eval()

This is the shorthand to perform this task. This converts the string to desired tuple internally.

## Python3

 `# Python3 code to demonstrate working of``# Convert String to Tuple``# Using eval()` `# initialize string``test_str ``=` `"``1``, ``-``5``, ``4``, ``6``, ``7``"` `# printing original string``print``("The original string : " ``+` `str``(test_str))` `# Convert String to Tuple``# Using eval()``res ``=` `eval``(test_str)` `# printing result``print``("``Tuple` `after getting conversion ``from` `String : " ``+` `str``(res))`

Output :

```The original string : 1, -5, 4, 6, 7
Tuple after getting conversion from String : (1, -5, 4, 6, 7)```

Time Complexity: O(1)
Auxiliary Space: O(1)

Method #3: Using split and list comprehension

## Python3

 `t ``=` `"1, -5, 4, 6, 7"``print``(``"The original string :"``, end ``=` `' '``)``print``(t)``s ``=` `t.split(``", "``)``x ``=` `[``int``(i) ``for` `i ``in` `s]``print``(``"Tuple after getting conversion from String :"``, end ``=` `' '``)``print``(``tuple``(x))`

Output

`(1, -5, 4, 6, 7)`

Time complexity: O(n)

The program splits the input string into a list of substrings using the split function, which takes O(n) time where n is the length of the input string. Then, the program iterates over each element of the list to convert it into an integer, which also takes O(n) time. Finally, the program creates a tuple from the list, which takes O(n) time as well. Therefore, the overall time complexity of the program is O(n).

Auxiliary Space: O(n)

The program creates a list of integers using the x variable, which takes O(n) space where n is the length of the input string. Then, the program creates a tuple from the list, which also takes O(n) space. Therefore, the overall auxiliary space complexity of the program is O(n).

Method 4: Using split() + tuple()

We first split the input string into a list of substrings using split(), and then convert each substring into an integer using a list comprehension. Finally, we create a tuple from the integer list using the tuple() function.

## Python3

 `test_str ``=` `"1, -5, 4, 6, 7"` `# split the string into a list of strings using comma and space as separators``str_list ``=` `test_str.split(``', '``)` `# create a new list by converting each element of the string list to an integer``int_list ``=` `[``int``(i) ``for` `i ``in` `str_list]` `# create a tuple from the integer list``res ``=` `tuple``(int_list)` `# print the original string and the converted tuple``print``(``"The original string: "` `+` `str``(test_str))``print``(``"Tuple after getting conversion from String: "` `+` `str``(res))`

Output

```The original string: 1, -5, 4, 6, 7
Tuple after getting conversion from String: (1, -5, 4, 6, 7)```

Time Complexity: O(n)
Auxiliary Space: O(n)

METHOD 5 : using the re (regular expression) module.

Here are the steps:

1. Import the re module using import re.
2. Initialize the input string test_str = “1, -5, 4, 6, 7”.
3. Use the re.split() function to split the string into a list of strings using , as the separator. This will give us str_list = re.split(‘, ‘, test_str).
4. Initialize an empty list int_list.
5. Use a for loop to iterate over each element of str_list.
6. Within the loop, use the int() function to convert the current string element to an integer and append it to int_list.
7. Use the tuple() constructor to create a tuple from the integer list. This will give us res = tuple(int_list).
8. Print the original string and the converted tuple using print(“The original string: ” + str(test_str)) and
9. print(“Tuple after getting conversion from String: ” + str(res)) respectively.

## Python3

 `import` `re` `test_str ``=` `"1, -5, 4, 6, 7"``str_list ``=` `re.split(``', '``, test_str)``int_list ``=` `[]``for` `s ``in` `str_list:``    ``int_list.append(``int``(s))``res ``=` `tuple``(int_list)``print``(``"The original string: "` `+` `str``(test_str))``print``(``"Tuple after getting conversion from String: "` `+` `str``(res))`

Output

```The original string: 1, -5, 4, 6, 7
Tuple after getting conversion from String: (1, -5, 4, 6, 7)```

Time complexity: The time complexity of this approach is O(n), where n is the number of elements in the input string.
Auxiliary space: The auxiliary space required by this approach is O(n), where n is the number of elements in the input string, due to the creation of the str_list and int_list lists.

Method 6:   Using the reduce() function.

Algorithm:

1. Import the reduce function from the functools module.
2. Initialize the test string.
3. Apply the split() method on the test string to convert it into a list of strings.
4. Using the reduce() function, convert each string element into an integer and accumulate them into a tuple.
5. Print the original string and the tuple of integers.

## Python3

 `from` `functools ``import` `reduce` `test_str ``=` `"1, -5, 4, 6, 7"``res ``=` `tuple``(``reduce``(``lambda` `acc, x: acc ``+` `(``int``(x),), test_str.split(``", "``), ()))` `print``(``"The original string: "` `+` `str``(test_str))``print``(``"Tuple after getting conversion from String: "` `+` `str``(res))``#This code is contributed by Jyothi pinjala.`

Output

```The original string: 1, -5, 4, 6, 7
Tuple after getting conversion from String: (1, -5, 4, 6, 7)```

Time Complexity: The time complexity of this code is O(n), where n is the number of elements in the input string. The split() method has a time complexity of O(n) as it needs to iterate over the entire string.

Auxiliary Space: The space complexity of this code is O(n), where n is the number of elements in the input string. We create a list to store the individual string elements, and then we convert them into a tuple. However, the space occupied by the tuple is negligible as compared to the list.

Method 7: Using heapq method:

Algorithm:

1. Split the string into a list of strings using comma and space as separators
2. Create a new list by converting each element of the string list to an integer
3. Create a tuple from the integer list
4. Print the original string and the converted tuple

## Python3

 `import` `heapq` `test_str ``=` `"1, -5, 4, 6, 7"` `# split the string into a list of strings using comma and space as separators``str_list ``=` `test_str.split(``', '``)` `# create a new list by converting each element of the string list to an integer``int_list ``=` `[``int``(i) ``for` `i ``in` `str_list]` `# create a tuple from the integer list``res ``=` `tuple``(int_list)` `# print the original string and the converted tuple``print``(``"The original string: "` `+` `str``(test_str))``print``(``"Tuple after getting conversion from String: "` `+` `str``(res))``#This code is contributed by Rayudu.`

Output

```The original string: 1, -5, 4, 6, 7
Tuple after getting conversion from String: (1, -5, 4, 6, 7)
```

Time complexity:
The time complexity of the code depends on the size of the input string. The str.split() method has a time complexity of O(n), where n is the length of the string. The list comprehension used to convert the string list to an integer list also has a time complexity of O(n). Finally, the tuple() function has a time complexity of O(n) as well, since it needs to iterate over the entire integer list to create the tuple. Therefore, the overall time complexity of the code is O(n).

Space complexity:
The space complexity of the code depends on the size of the input string. The str.split() method creates a new list of strings, which has a space complexity of O(n). The list comprehension creates another list of integers, which also has a space complexity of O(n). Finally, the tuple() function creates a new tuple, which has a space complexity of O(n) as well, since it contains all the integers from the input string. Therefore, the overall space complexity of the code is O(n).