Answers for "how to convert string to tuple in python"

1

python convert a string of tuples to a tuple of tuples

# Basic syntax:
import ast
ast.literal_eval(your_string)

# Example usage:
# Say you want to convert a string like:
'(0,0,0), (0,0,1), (1,1,0)' # or like
'((0,0,0), (0,0,1), (1,1,0))'
# to a tuple of tuples like:
((0,0,0), (0,0,1), (1,1,0))

# Import the Abstract Syntax Trees package:
import ast
your_string = '(0,0,0), (0,0,1), (1,1,0)'

# Convert to tuple of tuples:
your_tuple = ast.literal_eval(your_string)
print(your_tuple)
--> ((0,0,0), (0,0,1), (1,1,0))
Posted by: Guest on April-28-2021
1

string to tuple python

string = "mystring"
tuple1 = tuple(string)
# ('m', 'y', ' ', 's', 't', 'r', 'i', 'n', 'g')

tuple2 = string,
# ("my string",)
Posted by: Guest on March-18-2021
4

python string to tuple

l = list('abcd')  	        # ['a', 'b', 'c', 'd']
l = map(None, 'abcd')       # ['a', 'b', 'c', 'd']
l = [i for i in 'abcd']	    # ['a', 'b', 'c', 'd']

import re               # importing regular expression module
l = re.findall('.', 'abcd')
print(l)                	# ['a', 'b', 'c', 'd']
Posted by: Guest on May-13-2021

Code answers related to "how to convert string to tuple in python"

Python Answers by Framework

Browse Popular Code Answers by Language