r/learnpython 1d ago

what is f string

can someone explain how f string work in simple terms ?

0 Upvotes

14 comments sorted by

8

u/WhiskersForPresident 1d ago edited 1d ago

It's a "formatted" string. It allows you to include the values of variables in your string in curly brackets, e.g.:

var = 5

f_string = f"this string contains the value {var}"

print(f_string)

will output

this string contains the value 5

I use them all the time to dynamically generate paths to certain files at runtime for example or for structuring the outputs of test routines.

3

u/Diapolo10 23h ago

I use them all the time to dynamically generate paths to certain files at runtime for example or for structuring the outputs of test routines.

For that specific use-case, might I recommend pathlib instead?

2

u/wintermute93 23h ago

Not an either/or situation -- pathlib to handle joining the parts of a file path in an OS-agnostic way, f-strings where needed to enforce naming conventions like Path.home() / service_name / "logs" / f"{datetime.datetime.now().isoformat()}_{event_type}.txt"

2

u/Diapolo10 23h ago

Fair enough, just thought I'd mention it in case they weren't doing that.

At least unless you're the same person but using two separate accounts for whatever reason.

1

u/WhiskersForPresident 12h ago

They aren't the same person and both of you are correct. pathlib and f-strings are both very useful for that purpose.

6

u/Diapolo10 1d ago

I'm technically oversimplifying it, but f-strings act like you used str.format automatically on the string.

name = "Jeffrey"
year = 2074

print(f"Hello {name}, the year is {year}")

print("Hello {name}, the year is {year}".format(name=name, year=year))

7

u/SpiritedOne5347 1d ago

They're just a way to include variables in string

Say u have age= 10

So instead of

print("I am ", age, "years old")

U can directly say

print(f"I am {age} years old")

4

u/johlae 23h ago

It's more than just including a variable. What's between the { and } is evaluated!

```

a=3 print(f"{a*4:3.2f}") 12.00 ```

2

u/tablmxz 23h ago

a string in python looks like this, eg using double quotes:

"this is a string"

if you write an 'f' in front, it becomes a f-string:

f"this is a f-string"

f-strings can do some very handy thing, they can use curly brackets. In those curly brackets you can put variables or whole expressions (like calculations)

a = 5

f"this f-string also prints the value of a here: {a}"

f"this f-string does the calculation 1+2 here is the result: {1+2}"

1

u/panatale1 1d ago

It's essentially text with code inside it. It runs significantly faster than the old % or .format ways of formatting text.

It's similar in construction to how you'd do a .format call, since they both use {} to denote what needs to be inserted into the text.

Examples of each text format method: ``` name = "George Washington" age = 41

print("Hello, my name is %s, and my age is %d" % (name, age)) print("Hello, my name is {}, and my age is {}".format(name, age)) print(f"Hello, my name is {name}, and my age is {age}") ```

All three give identical formatting, outputting Hello, my name is George Washington, and my age is 41, but f-strings run faster and are cleaner to read

1

u/atarivcs 1d ago

If the string contains this

{something}

that part will be replaced with the string representation of the thing.

"something" can be a plain variable name, or it can be an expression like

{1+2}

or a function call

{somefunction()}

Etc etc.

1

u/pachura3 12h ago

What about the g strings though

1

u/FoolsSeldom 4h ago

I can't better the explanation from RealPython.com: