Python String to Tuple: How to convert

The string is a group of characters and it is the most useful object in the coding. But using the tuple you can manipulate the string more easily. To do so you have to first convert the Python string to a tuple in Python. In this step, you will know all the steps to convert the Python string to tuple.

Steps to convert Python String to Tuple

Let’s know all the steps that will lead to the conversion of the string to a tuple.

Step 1: Define a string

The first step is to define the string that you want to convert to a tuple. You can do this by assigning a string to a variable using single quotes, double quotes, or triple quotes. Below is the my_string variable that contains a string.

my_string = 'Code the Best'

Step 2: Use the split() method

The next step is to split the string. The split() method is the best way to split a string into a list of substrings based on a specified separator. In this case, the separator is a space character. To split the string into individual words, you just call the split() method on the string.

my_list = my_string.split()

You will get the list of substrings after splitting.

[Code, the, Best]

Step 3: Convert the list to a tuple

Now you have the list of substrings, you have to convert it to a tuple using the tuple() function. The tuple() function takes an iterable (in this case, a list) and returns a tuple containing the same elements.

my_tuple = tuple(my_list)

Step 4: Print the tuple

At last, you can print the tuple to verify that the conversion was successful. You can display the result using the print() function. Just pass the tuple as an argument.


Below is the complete code.

my_string = 'Code the Best'
my_list = my_string.split()
my_tuple = tuple(my_list)


(Code, the, Best)


Manipulation of a string becomes very easy if you convert the string to a tuple. The above are the easy steps for converting the string to a tuple. Here the separator is space, but the separate can be any character. For this, the process is the same. You have to just pass the appropriate delimiter argument in the split() function.

Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.


This Offer is Limited! Grab your Discount!
15000 ChatGPT Prompts
Offer Expires In: