String to tuple Python
In this post, you will learn how to convert a string to a tuple using the Python programming language.
A Python string is the sequence of characters surrounded by single quotes, double quotes, or triple quotes. Strings are immutable, indexable, and iterable.
A tuple is a sequence. Python tuples work exactly like a list, except tuples are immutable, which means they cannot be changed in place. The tuples are written inside parentheses. The tuple length is always fixed.
Python convert string to tuple using map() and split() functions
Python's map() is an in-built function. It applies a given function to each item of an iterable (list, tuple, etc.) and returns an iterator. The split() method of Python splits a string into a list. In this, we define a string and print it on console. We just split each element of a string, and every element is converted to an integer, and this operation is applied to every element using the 'map' method. Then we convert the list to the resultant tuple.
# Python code to
# convert String to Tuple
# using map() + tuple() + int + split()
# initialize string
str1 = "5, 4, 9, -1, 3"
# printing original string
print("Original string: " + str(str1))
# Convert String to Tuple
res_tuple = tuple(map(int, str1.split(', ')))
print("Tuple after getting conversion from string : " + str(res_tuple))
Output of the above code:
The original string: 5, 4, 9, -1, 3
Tuple after getting conversion from String : (5, 4, 9, -1, 3)
Python convert string to tuple using eval() function
Python eval() function parse the expression argument and evaluate it as a python expression and runs python expression within the program.
Syntax-eval(expression, globals, locals)
expression: The string is parsed and evaluated as a Python expression.
globals: A dictionary that specifies the available global methods and variables.
locals: It is another dictionary which is commonly used for mapping type in Python.
The given Python program converts the string to desired tuple internally.
# Python code to
# convert String to Tuple
# Using eval()
# initialize string
str1 = "6, -2, 7, 3, 4"
# printing original string
print("The original string: " + str(str1))
# Convert String to Tuple
# Using eval()
result = eval(str1)
# printing result
print("Tuple after getting conversion from string: " + str(result))
Output of the above code:
The original string: 6, -2, 7, 3, 4
Tuple after getting conversion from string: (6, -2, 7, 3, 4)
Related Articles
Python Convert String to UppercaseConvert string to int Python
Python program to reverse a string
Count consonants in a string Python
Python split strings by comma
Count vowels in a string Python
Convert string to lowercase Python
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python iterate list with index
Convert List to Dataframe Python
numpy random choice
Dictionary inside list python
Strip function in Python
Python raise keyword
Check if two strings are anagrams Python
Python program to map two lists into a dictionary