Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how to convert the a sequece of numbers seperated with comma into tuple

+5 votes
asked Apr 8, 2023 by 22B01A4578 AI & DS (170 points)

3 Answers

0 votes
answered May 12, 2023 by Fanish Pandey (140 points)

I am having a string options="1110000000" and I want to convert it to comma separated tuple as follows options_converted = ('1', '1', '1', '0', '0', '0', '0', '0', '0', '0')

With the help of this code we can do

options_converted = ()
for i, j in enumerate(options):
    options_converted = options_converted + str(j)

.

0 votes
answered May 16, 2023 by Hope (300 points)

There are two ways to convert a sequence of numbers separated with comma into tuple.

i) By split() method

ii) By eval() function

Take an example sequence : 1,2,3,4,5

i)

seq_of_numbers = "1, 2, 3, 4, 5"

tuple_of_numbers = tuple(seq_of_numbers.split(","))

print(tuple_of_numbers)

ii)

seq_of_numbers = "1, 2, 3, 4, 5"

tuple_of_numbers = eval(seq_of_numbers)

print(tuple_of_numbers)

Thats's it !

0 votes
answered May 20, 2023 by Sacha Bugnon (240 points)
In Python you can use the tuple() constructor. If you have a sequence as a list [1,3,5,7], or as a set {1,3,5,7}, doing tuple([1,3,5,7]), or tuple({1,3,5,7}), will return the tuple (1,3,5,7).
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...