>>> list=['Hello']
>>> tuple(list)
('Hello',)
Why is the result of the above statements ('Hello',)
and not ('Hello')
?. I would have expected it to be the later.
You've got it right. In python if you do:
a = ("hello")
a
will be a string since the parenthesis in this context are used for grouping things together. It is actually the comma which makes a tuple, not the parenthesis (parenthesis are just needed to avoid ambiguity in certain situations like function calls)...
a = "Hello","goodbye" #Look Ma! No Parenthesis!
print (type(a)) #<type 'tuple'>
a = ("Hello")
print (type(a)) #<type 'str'>
a = ("Hello",)
print (type(a)) #<type 'tuple'>
a = "Hello",
print (type(a)) #<type 'tuple'>
And finally (and most direct for your question):
>>> a = ['Hello']
>>> b = tuple(a)
>>> print (type(b)) #<type 'tuple'> -- It thinks it is a tuple
>>> print (b[0]) #'Hello' -- It acts like a tuple too -- Must be :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With