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 String Methods

jordan tucker
jordan tucker
2,304 Points

Hey, can someone show me a snippet example of the .format method in the text editor rather than the REPEL, please.

I'm a visual learner and I'm getting slightly confused with the .format method when written in the REPEL, so could someone please take a snippet of an example with it written on the actual text editor.

thanks in advance

2 Answers

Hi Jordan

Here is a short example I made in PyCharm:

lang = "Python"

# using .format()
print("I love {} programming".format(lang))

"""
OUTPUT:

I love Python programming
"""

Another great alternative to .format() is f-string which was introduced in Python 3.6. Here's an example:

lang = "Python"

# using f-string
print(f"I love {lang} programming")

"""
OUTPUT:

I love Python programming
"""

With f-string you can write the variable name right inside the curly brackets {} which makes the code clearer and easier to follow. You can google it - in my opinion it is really an excellent feature!

I hope this answers your question!

jordan tucker
jordan tucker
2,304 Points

Thank you so much Ave, you've really cleared that up for me.

Happy to help, Jordan! :-) Thank you for the positive feedback.

jordan tucker
jordan tucker
2,304 Points

You are very much welcome Ave :-)