In this tutorial, we will look at how to convert a tuple to an integer in Python with the help of some examples.
How to convert a tuple to an integer in Python?
Depending on what exactly you want, there can be several ways to convert a tuple to an integer. For example – If you want to convert the tuple (1,2,3)
to the integer 123
, you can do so by concatenating the digits as strings into a single string and then convert it to an integer.
Let’s look at an example.
# create a tuple t = (1, 2, 3) # tuple to integer num = int("".join(map(str, t))) print(num)
Output:
123
Here we create a single integer from a tuple of integers. We use the map()
function to convert each element in the tuple to a string. Then we use the string join()
function to concatenate the string digits. Finally, we convert the resulting string to an integer using the int()
function.
Alternatively, you can also use a list comprehension instead of the map()
function to convert each element to a string.
# create a tuple t = (1, 2, 3) # tuple to integer num = int("".join([str(i) for i in t])) print(num)
Output:
123
We get the same result as above.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Unpack elements of a tuple to variables
If, on the other hand, you just want to unpack the elements of a tuple to different variables, you can use the assignment operator =
.
# create a tuple person = ("Jim", 23) # unpack the tuple name, age = person print(name) print(age)
Output:
Jim 23
Here, we unpack the tuple ("Jim", 23)
such that the variable name
stores the first element, "Jim"
and the variable age
stores the second element, "23"
.
How to convert an element from a tuple to int?
If you’re looking to convert a particular element from a tuple to an integer, you can do simply use the Python built-in int()
function on the respective element.
For example, if you have a tuple of string digits and you want to convert the first element to an integer.
# create a tuple t = ("7", "11") # convert first tuple element to int num = int(t[0]) print(num) print(type(num))
Output:
7 <class 'int'>
Here we access the first element using its index and then pass it to the int()
function. We get the resulting value as an integer.
You might also be interested in –
- Convert Tuple to a Set in Python – With Examples
- Tuple to a Dictionary in Python
- Convert Tuple to a String in Python
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.