Tlachi
PRO
last year
Tlachicountry asked

In Python, why do we need to use str(pen_number)?

Abhay Jajodia
Expert
last year
Abhay Jajodia answered

Hi Tlachi,

Because pen_number is a number, and "Pens for each student: " is a string. In Python, you cannot join a string and a number using + unless you convert the number to a string first.

So this works:

print("Pens for each student: " + str(pen_number))

Another easy option is to use commas, which avoids str():

print("Pens for each student:", pen_number)

If you have more questions, I am here to help 😊

Python
This question was asked as part of the Getting started with Python course.