Python | Convert tuple to float value

Sometimes, while working with tuple, we can have a problem in which, we need to convert a tuple to floating-point number in which first element represents integer part and next element represents a decimal part. Let’s discuss certain way in which this can be achieved.

Method : Using join() + float() + str() + generator expression

The combination of above functionalities can solve this problem. In this, we 1st convert the tuple elements into a string, then join them and convert them to desired integer.



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Convert tuple to float
# using join() + float() + str() + generator expression
  
# initialize tuple
test_tup = (4, 56)
  
# printing original tuple 
print("The original tuple : " + str(test_tup))
  
# Convert tuple to float
# using join() + float() + str() + generator expression
res = float('.'.join(str(ele) for ele in test_tup))
  
# printing result
print("The float after conversion from tuple is : " + str(res))

chevron_right


Output :

The original tuple : (4, 56)
The float after conversion from tuple is : 4.56


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.