r/learnpython • • Jun 07 '21

TIL I’ve been making debugging statements harder than they needed to be.

I don’t know if I’m the only one who missed this, but today I learned that adding an "=" sign to the end of an f-string variable outputs "variable_name=value" rather than just the "value"

Makes writing quick, clean debug statements even easier!

In [1]: example_variable = [1,2,3,4,5,6]

In [2]: print(f"{example_variable=}")
example_variable=[1, 2, 3, 4, 5, 6]

In [3]:

Edit: Works in Python 3.8+, thanks /u/bbye98

857 Upvotes

91 comments sorted by

View all comments

174

u/AI-Learning-AI Jun 07 '21

f strings are awesome.

139

u/muzunguman Jun 08 '21

2nd only to g strings

46

u/sqjoatmon silentquasar Jun 08 '21

Someone needs to submit a PEP next April 1.

24

u/FancyGUI Jun 08 '21

“Next, in python 4!”

12

u/[deleted] Jun 08 '21

https://www.python.org/dev/peps/pep-0401/

They've got April Fool's covered.

6

u/uncanneyvalley Jun 08 '21

I love that it’s PEP 04/01, too. Peak nerd humor, I love it.

→ More replies (1)

18

u/Windows_XP2 Shitty Programmer Jun 08 '21

Why is an f string better than something like print("String"+myvar+"String")?

76

u/[deleted] Jun 08 '21

easier to use, much easier to read, sometimes faster

5

u/synthphreak Jun 08 '21 edited Jun 08 '21

Beyond u/Al3xR3ads' points above, f-strings also allow some additional formatting options beyond what is possible with print, or at least makes them much easier to do. For example:

>>> # formatting numbers
>>> x = 0.123
>>> f'{x:.2%}'
'12.30%'
>>> f'{10**8:,}'
'100,000,000'
>>> 
>>> 
>>> # custom fixed-width spacing
>>> hello = 'hello'
>>> f'well {hello:^25} there'
'well           hello           there'
>>> 
>>> 
>>> # right/left/center justification with filler
>>> f'{hello:*>10}'
'*****hello'
>>> f'{hello:*>15}'
'**********hello'
>>> f'{hello:*<20}'
'hello***************'
>>> f'{hello:~^20}'
'~~~~~~~hello~~~~~~~~'

21

u/[deleted] Jun 08 '21

From my understanding, there's no need to do formatting so you don't need to convert integers to strings and f strings run faster. Someone more experienced can probably give you a more in depth explanation but that's just my take on it

13

u/_maxt3r_ Jun 08 '21

Less characters to write. And it supports formatting like

{some_float:.3f} will format the number with 3 decimals

6

u/appliku Jun 08 '21

wow, cool, never thought to use that tbh. Thanks for this valuable comment!

→ More replies (4)

7

u/KingGarrettFTW Jun 08 '21

Use and F string and you'll realize. You just put {statements} and it runs inside of the string. Genius!

3

u/Windows_XP2 Shitty Programmer Jun 08 '21

Do f strings also work for variables? If they do then I'll find them very useful.

4

u/jaber24 Jun 08 '21

Yeah you can use them anywhere you use regular strings.

4

u/scrdest Jun 08 '21

The '+' approach is extremely inefficient. Since strings are immutable, A + B + C takes two strings of lengths a/b and produces a new string of length a+b, then takes that new string and a string of length c and produces a fifth string of length a+b+c, etc. etc.

So, if you have a lot of strings/variables concatenated together, this approach wastes a ton of time creating temporary new strings and copying them over and over.

Also, f-strings always formats data to strings properly. Adding stuff to a string may have weird results depending on how the operator is defined for the thing you're trying to write.

→ More replies (2)

2

u/Se7enLC Jun 08 '21

Discovered them, converted all my code to use them, regretted instantly when the machine I needed to use didn't have a new enough Python version.

I'm fine with having a minimum required version to use something I've written, but "because I wanted a different print function" is a pretty bad reason.

(I think most distributions have updated by now, though)

4

u/AI-Learning-AI Jun 08 '21

But it was easy for you to rollback your code because you have excellent version control!

4

u/Se7enLC Jun 08 '21

More like, I didn't do every switchover to f-strings in a single commit so I was fscked.

→ More replies (5)

1

u/[deleted] Jun 08 '21

They are awesome but a small peeve - you cannot do:

var = "hello world"    
print(f"{
    var
}")

13

u/deja_entenduu Jun 08 '21

You can use multi-line quotes.

`print(
   f“””
   {var}
   Text
   “””
)`
→ More replies (1)

3

u/backdoorman9 Jun 08 '21

But you can do:

print(
    f"{var_1}",
    f"{var_2}"
)
→ More replies (4)
→ More replies (1)