Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics Types and Branching Numeric

What is the purpose of the "int" function?

I thought the "int" function allowed the user to print numbers in a string, but Im able to print numbers without using "int".

2 Answers

The purpose of the int() function is so that you may use integers like this for instance from a users input:

Ask user the amount of gross money earned per month

monthly_pay = int(input("How much do you make per month? "))

Simple formula calculating the yearly pay by multiplying by 12 months to get the total yearly income

annual_pay = monthly_pay * 12

Displaying the total annual pay for the user to the console

print(f'Your annual pay is ${annual_pay:,.2f}')

Save this code in your workspace and name it whatever you would like for instance money_Display.py then run the program like this in the workspace console python money_Display

treehouse:~/workspace$ python money_Display.py
How much do you make per month? 4503
Your annual pay is $54,036.00

Steven Parker
Steven Parker
231,264 Points

The "print" function actually works best on strings, but is smart enough to handle numbers.

You'd be more likely to need "int" if you were doing math on numbers in a string.

For more details, see the online manual page on int().