Convert Range into a Tuple in Python

Convert Range into a Tuple

To convert a given range object into a Tuple in Python, you can use tuple() builtin function. Call the tuple() function and pass the given range object as argument. The tuple() function creates a new tuple object using the values from the given range object, and returns it.

The syntax to convert given range object myrange into a tuple mytuple is

mytuple = tuple(myrange)

Examples

1. Convert range(start, stop) into a tuple

In the following example, we take a range object with a start of 4, and stop of 10, and convert this range object into a tuple.

Python Program

# Take a range
myrange = range(4, 10)

# Convert range object into a tuple
mytuple = tuple(myrange)
print(mytuple)
Run Code Copy

Output

(4, 5, 6, 7, 8, 9)

2. Convert range(start, stop, step) into a tuple

In the following example, we take a range object with a specific step value, and convert this range object into a tuple.

Python Program

# Take a range
myrange = range(4, 15, 3)

# Convert range object into a tuple
mytuple = tuple(myrange)
print(mytuple)
Run Code Copy

Output

(4, 7, 10, 13)

Summary

In this tutorial of Python Ranges, we learned how to convert a range object into a Tuple of integers using tuple() builtin function.

Related Tutorials

Code copied to clipboard successfully 👍