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 (2015) Logic in Python Having a Conversation

Inputs are returning numbers as opposed to strings.

Why is this happening? I thought all inputs return strings in Python?

>>> age = input("What's your age?")
What's your age? 30
>>> age
30

[MOD: added ```bash markdown formatting -cf]

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

In Python 2, input is treated as evaluated code. So a number stays a number and strings must be quoted. Python 2 also had a raw_input() that treat the input as raw text string, and return it without the trailing newline char. Due to the inherent risky nature of evaluating direct input from users, In Python 3 raw_input() was renamed to input() and the original Python 2 input() was eliminated. For those still wanting the original Python 2 functionality in Python 3, they can use eval(input()).

$ python2
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> age = input("What's your age?")
What's your age?30
>>> age
30

$ python3
Python 3.4.3 (default, Oct 14 2015, 20:28:29) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> age = input("What's your age?")
What's your age?30
>>> age
'30'

EDIT: clarified transition for input from Python 2 to 3.

Thank you. This seems preposterous though. Why not make numbers numbers, strings strings, and so forth? A string shouldn't be specified it is a string until it's giving quotes, similar to Javascript. Why does Python do this differently? And why'd they make this specific change from 2 to 3?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

In practice, most Python 2 coders switched to using raw_input() because knowing your are starting with a received string allows the programmer to control the parsing and conversion. It is also more natural for users to be able to enter text strings without needing to added quotation marks. Because of this, Python 3 made the switch to make this the default input() behavior.

It is only during input from users that this happens. Elsewhere in the code numbers are number and strings are strings, etc.

Maybe we're not referring to the same thing. I'm confused by your comment. Where in JavaScript are you receiving input where numbers are numbers and strings are strings?

Thank you for the detailed answer!