Convert String to Tuple in Python

  • Post category:String

In Python, the tuple() function is used to convert a string to tuple. The tuple() function returns a tuple of characters in the string you specify. The tuple() function is useful when you want to iterate over a string. Let’s see how to use the tuple() function to convert a string to a tuple.

Step 1: Create a string

str = "CodeAllow"
print("str = ", str)
print("str type = ", type(str))

Step 2: Convert string to tuple

tpl = tuple(str)
print("tuple = ", tpl)
print("tuple type = ", type(tpl))

Output:

str =  CodeAllow
str type =  <class 'str'>
tuple =  ('C', 'o', 'd', 'e', 'A', 'l', 'l', 'o', 'w')
tuple type =  <class 'tuple'>

Free resources to learn advanced skills: AiHints and CodeAllow