# Convert a tuple to an integer in Python Last updated: Jul 2, 2022 Photo from Unsplash

## Convert a tuple to an integer in Python#

There are multiple ways to convert a tuple to an integer:

1. Access a tuple element at its index and convert it to an int, e.g. `int(my_tuple)`.
2. Sum or multiply the elements of the tuple.
3. Convert a tuple of strings to a tuple of integers.
main.py
```Copied!```# ✅ access tuple element and convert it to an integer
my_tuple_1 = ('1', '3', '5')

my_integer = int(my_tuple_1)
print(my_integer)  # 👉️ 1

# -------------------------------------------------

# ✅ sum or multiply the elements of a tuple to get an integer
my_tuple_2 = (2, 4, 6)

result = sum(my_tuple_2)

print(result)  # 👉️ 12

# -------------------------------------------------

# ✅ convert a tuple of strings to a tuple of integers

my_tuple_3 = ('1', '3', '5')

tuple_of_integers = tuple(int(item) for item in my_tuple_3)

print(tuple_of_integers)  # 👉️ (1, 3, 5)
``````

The first example accesses the tuple element at a specific index and uses the `int()` class to convert it to an integer.

main.py
```Copied!```my_tuple_1 = ('1', '3', '5')

my_integer = int(my_tuple_1)
print(my_integer)  # 👉️ 1
``````
Python indexes are zero-based, so the first element in a tuple has an index of `0`, the second an index of `1`, etc.

When the index starts with a minus, we start counting backwards from the end of the tuple. For example, the index `-1` gives us access to the last element, `-2` to the second-last, etc.

main.py
```Copied!```my_tuple_1 = ('1', '3', '5')

my_integer = int(my_tuple_1[-1])
print(my_integer)  # 👉️ 5
``````

You only have to use the `int()` class if your tuple doesn't store integers. Otherwise, directly access the tuple element at its index.

main.py
```Copied!```my_tuple_1 = (1, 3, 5)

my_integer = my_tuple_1
print(my_integer)  # 👉️ 3
``````

You can also convert a tuple to an integer by using the `sum()` function or multiplying its values.

main.py
```Copied!```import math

# ✅ sum elements of a tuple
my_tuple_2 = (2, 4, 6)

sum_result = sum(my_tuple_2)
print(sum_result)  # 👉️ 12

# ✅ multiply elements of a tuple

multiplication_result = math.prod(my_tuple_2)
print(multiplication_result)  # 👉️ 48
``````

If you need to convert a tuple of strings to a tuple of integers, use a generator expression.

main.py
```Copied!```my_tuple_3 = ('1', '3', '5')

tuple_of_integers = tuple(int(item) for item in my_tuple_3)

print(tuple_of_integers)  # 👉️ (1, 3, 5)
``````
Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we pass the current tuple item to the `int()` class to convert it to an integer and return the result.

The last step is to use the `tuple()` class to convert the generator object to a tuple.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer. You can use the search field on my Home Page to filter through all of my articles.