Python Python Basics Types and Branching String Methods

jordan tucker
jordan tucker
1,949 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

Ave Nurme
Ave Nurme
14,826 Points

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
1,949 Points

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

Ave Nurme
Ave Nurme
14,826 Points

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

jordan tucker
jordan tucker
1,949 Points

You are very much welcome Ave :-)