• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Tuple String to Tuple in Python

Tuple String to Tuple in Python

Author: Aditya Raj
Last Updated: March 23, 2022

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.

Related

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.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us