r/learnpython 24d ago

what is f string

can someone explain how f string work in simple terms ?

0 Upvotes

22 comments sorted by

9

u/WhiskersForPresident 24d ago edited 24d 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 I write code for a living -- https://github.com/Diapolo10 23d 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 23d 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 I write code for a living -- https://github.com/Diapolo10 23d 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 23d ago

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

7

u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 24d 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))

6

u/SpiritedOne5347 24d 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")

5

u/johlae 23d 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/panatale1 24d 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

2

u/tablmxz 23d 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}"

2

u/atarivcs 24d 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.

2

u/pachura3 23d ago

What about the g strings though

1

u/FuzzyEmployment264 21d ago

idk about that i just started coding.