- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert String to Tuple in Python
When it is required to convert a string into a tuple, the 'map' method, the 'tuple' method, the 'int' method, and the 'split' method can be used.
The map function applies a given function/operation to every item in an iterable (such as list, tuple). It returns a list as the result.
The 'int' method converts the given data type to an integer type, if that operation is permitted.
The split method splits the given data into different sections based on a delimiter or a default delimiter. The 'tuple' method converts the given data type into a tuple type.
Below is a demonstration of the same −
my_str_1 = "7, 8, 0, 3, 45, 3, 2, 22, 4" print ("The string is : " ) print(my_str_1) my_result = tuple(map(int, my_str_1.split(', '))) print("The tuple after converting from a string is : ") print(my_result)
The string is : 7, 8, 0, 3, 45, 3, 2, 22, 4 The tuple after converting from a string is : (7, 8, 0, 3, 45, 3, 2, 22, 4)
- A string is defined and is displayed on the console.
- The string is split, and every element is converted to an integer, and this operation is applied to every element using the 'map' method.
- This is again converted to a tuple type.
- This result is assigned to a value.
- It is displayed as output on the console.
- How can I convert a Python tuple to string?
- Convert Tuple to integer in Python
- Convert tuple to float value in Python
- Convert location coordinates to tuple in Python
- Convert tuple to adjacent pair dictionary in Python
- Python program to convert Set into Tuple and Tuple into Set
- Convert Nested Tuple to Custom Key Dictionary in Python
- Python program to convert tuple into list by adding the given string after every element
- How to convert a list into a tuple in Python?
- How can I convert Python tuple to C array?
- How to convert JSON data into a Python tuple?
- Convert a list into tuple of lists in Python
- How to convert python tuple into a two-dimensional table?
- How can I convert a Python tuple to an Array?
- Python - To Convert Matrix to String