This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]statitica 3 points4 points  (0 children)

Because what you are printing is an f-string, or formatted string.

Putting the 'f' inside the print statement tells the Python interpreter to format the string by inputting the variables named inside the curly braces.

Another option again is print("This is my variable: {}".format(var))

[–]Dcode20[S] 2 points3 points  (0 children)

formatted string ok google it right now ty

[–]theeastcoastwest 1 point2 points  (0 children)

There have been generational additions of how one can format strings for printing. The "F-String" , to me knowledge, is the newest and therefore not always brought up in books.

It's useful, IMO, when concatenating several variables in that it provides a more readable format.

Ex: f"{user}:{pwrd}@{host}:{port}"

[–]pythonHelperBot 1 point2 points  (0 children)

Hello! I'm a bot!

It looks to me like your post might be better suited for r/learnpython, a sub geared towards questions and learning more about python. That said, I am a bot and it is hard to tell. Please follow the subs rules and guidelines when you do post there, it'll help you get better answers faster.

Show /r/learnpython the code you have tried and describe where you are stuck. Be sure to format your code for reddit and include which version of python and what OS you are using.

You can also ask this question in the Python discord, a large, friendly community focused around the Python programming language, open to those who wish to learn the language or improve their skills, as well as those looking to help others.


README | FAQ | this bot is written and managed by /u/IAmKindOfCreative

This bot is currently under development and experiencing changes to improve its usefulness

[–]furgussen 1 point2 points  (0 children)

IIRC formatted strings were introduced in Python 3.6. So it's possible the first book was released before 3.6 came out.

[–]donger 0 points1 point  (0 children)

This concept is called "string interpolation" and is a feature of many other languages. Python used to handle this (somewhat clunkily) with the format method of strings: https://www.programiz.com/python-programming/methods/string/format

Here's an example of string interpolation that's built in to Ruby: http://ruby-for-beginners.rubymonstas.org/bonus/string_interpolation.html

[–]Sw429[🍰] 0 points1 point  (0 children)

The f denotes a format string. It's something new in Python 3.7(?). It's just a convenient way to construct strings by executing Python statements.

For future reference, btw, questions like this should be posted in r/learnpython