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

Using format(variable)

What is the difference betwen using: print("text", variable) and print("text {}".format(variable))

?

3 Answers

Both perform the same task but both perform it differently, one is concatenation and one is calling a method on the string. There is no right choice of the two, I prefer the format method as it is easier for me to read and work with, but concatenation is fine.

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,732 Points

Josh Keenan is correct. There is an essential similarity between the methods.

I totally recommend you get used to using the format method!

I use the format method frequently in intermediate and advanced programming-- the main advantage, besides readability, is it supports multiple substitution methods, like positional arguments and keyword arguments.

Format works in Python 2.x and 3.x and gives you some advantages when you are writing code that has to run independent of version. The print function is different in 2.x so the comma delimitation of a print will return a tuple rather than the expected format in 3.x.

Extra Whitespace

Format does not impose an extra whitespace character... there are lots of times you don't want that to be there.

price=12.34
print("Price is $",price) # imposes an extra space after '$' character
print("Price is ${}".format(price))
Price is $ 12.34
Price is $12.34

Positional arguments

Format allows REPEATING of an ordered parameter. Code is easier to write and less confusing to readers.

# position arguments
count=20
task="bungee jump"
print("He had {0} reasons to {1}, and {0} reasons not to {1}!".format(count, task))
print ("He had", count, "reasons to", task, ", and ", count, "reasons not to", task, "!")
He had 20 reasons to bungee jump, and 20 reasons not to bungee jump!
He had 20 reasons to bungee jump , and  20 reasons not to bungee jump !

Named parameters

Format allows you to name keyword arguments. You will realize the advantage in formating where you can call out a parameter by its name as in a template.

In the example below, you can use composition with a format string method. It's also cool that you can do multiline "template-like" objects.

madlib = """
{exclamation}! He said {adverb} as he jumped into his convertible {noun}
and drove off with this {adjective} wife.

He vowed that he would never say "{exclamation}" again in public.
Especially with his {adjective} wife.
""".format(exclamation="Ouch", adverb="stupidly", noun="flying-machine", adjective="brave")
print(madlib)
Ouch! He said stupidly as he jumped into his convertible flying-machine
and drove off with this brave wife.

He vowed that he would never say "Ouch" again in public.
Especially with his brave wife.

Thank You.