Converting data from one form to another is a tedious task. In this article, we will discuss two ways to convert a tuple string to a tuple in python.
How to Convert a Tuple String to a Tuple in Python
Suppose that we are given a tuple in the form of a string as follows.
myStr = "(1,2,3,4,5)"
Now, we have to create the tuple (1,2,3,4,5)
from the given string. For this, we will first remove the parenthesis and the comma “,”
character. For this, we will replace the commas with spaces and parenthesis with empty string using the replace()
method. The replace()
method, when invoked on a string, takes the character to be replaced as the first input argument and the new character as the second input argument. We will replace the “(”, “)”
, and the “,”
character using the replace()
method one by one as follows.
myStr = "(1,2,3,4,5)"
print("The tuple string is:", myStr)
myStr = myStr.replace("(", "")
myStr = myStr.replace(")", "")
myStr = myStr.replace(",", " ")
print("The output string is:", myStr)
Output:
The tuple string is: (1,2,3,4,5)
The output string is: 1 2 3 4 5
Now, we have obtained a string that contains numbers separated by spaces. To obtain the numbers from the string, we will now split the string using the split()
method. The split()
method, when invoked on a string, takes a character as an optional input argument and returns a list containing the elements after splitting the string at the specified character. If we do not give any character as an input argument, it splits the string at whitespaces.
We will split the string using the split()
method as follows.
myStr = "(1,2,3,4,5)"
print("The tuple string is:", myStr)
myStr = myStr.replace("(", "")
myStr = myStr.replace(")", "")
myStr = myStr.replace(",", " ")
myList = myStr.split()
print("The output list is:", myList)
Output:
The tuple string is: (1,2,3,4,5)
The output list is: ['1', '2', '3', '4', '5']
Now, we have obtained a list where all the numbers are present. But, they are present as strings. To obtain a list of integers, we will use the map()
function and the int()
function as follows.
myStr = "(1,2,3,4,5)"
print("The tuple string is:", myStr)
myStr = myStr.replace("(", "")
myStr = myStr.replace(")", "")
myStr = myStr.replace(",", " ")
myList = myStr.split()
myList = list(map(int, myList))
print("The output list is:", myList)
Output:
The tuple string is: (1,2,3,4,5)
The output list is: [1, 2, 3, 4, 5]
As we have obtained the list of integers, we will create a tuple from the list as shown below.
myStr = "(1,2,3,4,5)"
print("The tuple string is:", myStr)
myStr = myStr.replace("(", "")
myStr = myStr.replace(")", "")
myStr = myStr.replace(",", " ")
myList = myStr.split()
myList = list(map(int, myList))
myTuple = tuple(myList)
print("The output tuple is:", myTuple)
Output:
The tuple string is: (1,2,3,4,5)
The output tuple is: (1, 2, 3, 4, 5)
You can observe that we have converted the tuple string to a tuple using the replace()
method, split()
method, and the int()
function in python.
Tuple String to a Tuple Using The eval() Function in Python
The eval()
function is used to evaluate expressions. It takes a string as an input argument, traverses the string, and returns the output. We can directly convert the tuple string to a tuple using the eval()
function as shown below.
myStr = "(1,2,3,4,5)"
print("The tuple string is:", myStr)
myTuple = eval(myStr)
print("The output tuple is:", myTuple)
Output:
The tuple string is: (1,2,3,4,5)
The output tuple is: (1, 2, 3, 4, 5)
Conclusion
In this article, we have discussed two ways to convert a tuple string to a tuple in python. To learn more about strings, you can read this article on string formatting in python. You might also like this article on list comprehension in python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.