Print a range of numbers in Python

avatar

Borislav Hadzhiev

Last updated: Aug 29, 2022

banner

Photo from Unsplash

Print a range of numbers in Python #

Use unpacking to print a range of numbers on the same line, e.g. print(*range(1, 11)). The iterable unpacking operator enables us to unpack an iterable, e.g. a range, in the call to the print() function.

main.py
# 👇️ 1 2 3 4 5 6 7 8 9 10 print(*range(1, 11)) # 👇️ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(list(range(1, 11))) # 👇️ [1, 3, 5, 7, 9] print(list(range(1, 11, 2))) # 👇️ 1 2 3 4 5 6 7 8 9 10 for num in range(1, 11): print(num, end=' ')

The first example uses the iterable unpacking operator to print a range of numbers on the same line.

The * iterable unpacking operator enables us to unpack an iterable in function calls, in comprehensions and in generator expressions.

main.py
example = (*(1, 2), 3) # 👇️ (1, 2, 3) print(example)

The range class is commonly used for looping a specific number of times in for loops and takes the following arguments:

NameDescription
startAn integer representing the start of the range (defaults to 0)
stopGo up to, but not including the provided integer
stepRange will consist of every N numbers from start to stop (defaults to 1)

If you print a range() object directly, you won't see the numbers it contains.

main.py
print(type(range(1, 10))) # 👉️ <class 'range'>

Use the list() class to print a range object, e.g. print(list(range(1, 11))). The list() class will convert the range object to a list, so we can print the numbers the range contains.

main.py
# 👇️ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(list(range(1, 11)))

If you only pass a single argument to the range() constructor, it is considered to be the value for the stop parameter.

main.py
for n in range(5): print(n) result = list(range(5)) # 👇️ [0, 1, 2, 3, 4] print(result)
The example shows that if the start argument is omitted, it defaults to 0 and if the step argument is omitted, it defaults to 1.

If values for the start and stop parameters are provided, the start value is inclusive, whereas the stop value is exclusive.

main.py
result = list(range(1, 5)) # 👇️ [1, 2, 3, 4] print(result)

If the value for the stop parameter is lower than the value for the start parameter, the range will be empty.

main.py
result = list(range(1, 0)) # 👇️ [] print(result)

We passed a start of 1 and a stop of 0 to the range() class, so the range object is empty.

You can also use a for loop to print a range of numbers on the same line.

main.py
# 👇️ 1 2 3 4 5 6 7 8 9 10 for num in range(1, 11): print(num, end=' ')

We set the end argument to a space in the call to the print() function.

The end argument is printed at the end of the message.

By default, end is set to a newline character (\n).

If you omit the argument, each number in the range will get printed on a separate line.

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