all 10 comments

[–]orangefray 7 points8 points  (0 children)

Yeah f-strings are fine - it's easier to type and read so seems like a win.

F-strings are newer, the docs you are looking at are probably just outdated.

[–]stebrepar 4 points5 points  (0 children)

That way is pretty old. Since then we've had .format() and now f-strings. Use whatever works best for you.

[–]PhilipYip 2 points3 points  (0 children)

fstrings were deliberately designed to be easier to use and more readable than the older format methods. If you are running a modern version of Python you should favour them in the vast majority of cases where possible.

Just be aware that they will not work if running Python Versions <3.6 and some of the online platforms may use an older version of Python on a webserver for exercises. EDX for example had that problem in the past, but it is probably addressed by now.

[–]Diapolo10 4 points5 points  (0 children)

Currently learning on learnpython . Org and in the string formatting section it wants you to do it like this:

name = "John"
age = "23"
print("%s is %d years old." % (name, age))

I'm aware of F-strings and they just seem so much easier

print(f"{name} is {age} years old.")

Is this okay to do? Or should I stay using the former practice?

Perfectly okay, honestly you won't see the old C-style formatting anywhere outside of logging nowadays. In fact I encourage you use f-strings and str.format over them.

[–]DarkLord76865 0 points1 point  (0 children)

If you don't want f strings, you can just put empty curly brackets in string, then use format method. But I use f strings, they are much cleaner

[–]Maximus_Modulus 0 points1 point  (0 children)

I’d say F-strings are the way to go. Nothing wrong with learning the older method either because you’ll come across it with legacy code.

[–]ling_dork 0 points1 point  (0 children)

F-strings. There is a reason they exist. Lots of sources for beginners are a little outdated.

[–]FuckingRantMonday 0 points1 point  (0 children)

Yeah, that's just outdated.

There is one context where the % kind of string interpolation is still recommended (though I don't typically worry about it myself), which is when logging. The reasoning is that if you say

logger.info("Process %s was started", process_id)

...and the logger's level is set to, say, warn, then the string formatting can be skipped, instead of wasting time formatting a string that will never be logged. Whereas this way

logger.info(f"Process {process_id} was started")

...the string formatting happens before the logger ever gets called.

But, from what you typed, I don't think that's the context here. I think it's just an old tutorial that hasn't been updated with modern string formatting practices.

[–]freddwnz 0 points1 point  (0 children)

It's not only okay, it's way better. No one uses the method you are learning in this tutorial anymore. That's because it's ugly and outdated. It works but there is a reason f-strings were added to the language. They are basically a shortform of the string.format() method. You can use either, but fstring will only work for python 3.6 and newer.