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

Ahmed Ali
PLUS
Ahmed Ali
Courses Plus Student 3,018 Points

Continental Challenge, I need clarification on print(f"*{continent}")!

So my confusion is with the f, I don't know what the f does or represents.... I ended up just copying it into the code via the hint as I couldn't find a reference to it in the related videos...

The challenge is to print only the continents starting with A

Code being:

continents = [ 'Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia',]

for continent in continents: if continent[0] == 'A': print(f"*{continent}")

2 Answers

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

The "f" is for format or string format template,

Earlier versions of Python used different conventions. The "f" is the natural evolution, but nothing prevents you from using the older-style methods. But I really like the newer convention.

first_name = 'Ricky'
last_name = 'LeFleur'
# originally this would work, tried and true.
# can get really complex and doesn't support format styles
print('My name is ' + first_name + ' ' + last_name + '.')
# which is equivalent to (this works in all Python and supports format style parmeters)
print('My name is %s %s.' % (first_name, last_name) )
# this also works (this works in Python 2.7 and Python 3.4 and newer)
# also supports format parameters
print('My name is {} {}.'.format(first_name, last_name))
# this is equivalent too (but only works in Python 3.6 and newer)
# also supports format parameters, by far the most simple!
print(f'My name is {first_name} {last_name}.')

The output:

My name is Ricky LeFleur.
My name is Ricky LeFleur.
My name is Ricky LeFleur.
My name is Ricky LeFleur.
Lindsey Mote
seal-mask
.a{fill-rule:evenodd;}techdegree
Lindsey Mote
Data Analysis Techdegree Student 1,148 Points

Thank you for asking this, Ahmed! I wish there had been some explanation in the course. And thanks for explaining it, Jeff. This makes much more sense now.