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 Meet Python Variables

variable doubt

Hi there! If I write print(first_name, "how are you?") the result is ('Fefe', 'how are you?') with quotes, instead if I write print(first_name + " how are you?") the result is Fefe how are you?

As I've learned in JavaScript, you use a + but with (,) it shows with the quotes.

Any suggestion?

3 Answers

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,722 Points

The format method of the string is very handy-- definitely worth learning.

The string is a template and braces indicate "fill in the blank".

Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> template = "{}, {}, and {} are {}."
>>> print(template.format('Larry','Curly','Moe','the Three Stooges'))
Larry, Curly, and Moe are the Three Stooges.
>>> print(template.format('Huey','Dewey','Louie','nephews of Donald Duck'))
Huey, Dewey, and Louie are nephews of Donald Duck.
>>>

What's cool is you can also reference the braces with integers for a position. This allows you to reuse the items.

>>> print("My name is the real {0} {1}. But don't call me {1} {0}".format('Slim','Shady'))
My name is the real Slim Shady. But don't call me Shady Slim

And you can use named parameters too.

madlib = "{student} should be excused from attending {noun} class. {student} will be attending the {adjective} {event}"
>>> print(madlib.format(student='Bart Simpson',noun='History', adjective='Boring',event='Ballet'))
Bart Simpson should be excused from attending History class. Bart Simpson will be attending the Boring Ballet
>>> print(madlib.format(student='Harry Potter',noun='Potions', adjective='Exciting',event='Wizard Tournament'))
Harry Potter should be excused from attending Potions class. Harry Potter will be attending the Exciting Wizard Tournament
Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,722 Points

This is a pretty astute observation.

The way "print" is handled between Python 2.7.x and Python 3.x is a little different. In Python 2 it is considered a "print statement", whereas in Python 3.x it is called a "print function". The parentheses are not needed in Python2, but are required in Python 3.x. To get around the disparity, you can use the format method of the string, you can get the same behavior in both versions of Python.

In Python 2.x you are telling print to print a 'tuple'

Python 2.7.9 (default, Dec 10 2014, 12:28:03) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> n='Fefe'
>>> print(n, 'how are you?')
('Fefe', 'how are you?')
>>>
>>> print("{} how are you?".format(n))
Fefe how are you?

In Python 3.x you are printing the results of two arguments concatenated.

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> n = 'Fefe'
>>> print(n,'how are you?')
Fefe how are you?
>>>
>>> print("{} how are you?".format(n))
Fefe how are you?

I've got a little dizzy with print("{} how are you?".format(n)) haha.

the {} indicates that the variable input it goes there?

Thanks a lot Jeff! I really got it now, amazing all this and I'd loved the examples! Thank you for the help and your time :)