DEV Community

Joe Neville
Joe Neville

Posted on

Use Python f-strings and never look back!

❓Are you using f-strings to format your strings in Python? Yes? Great.
🚀 If not, consider dropping your tired old format approach and get with the new(ish) stuff: f-strings.
📚 There's plenty of blogs out there that tell you all about f-strings. They tell you at length about the old approaches, % formatting and .format(), so I'm not going to go over all that, I'm just here to encourage you to take a look at f-strings.
🤔 Why? I'm using f-strings all the time now, and .format() seems clunky and overly verbose in comparison.

Here's the basics:

  • f-strings is the name given to 'formatted string literals'
  • Python 3.6+
  • Similar in approach to .format() but less verbose, which helps keep your line length down and improve code readability.

A few examples

Here's a simple combination of string variable declaration and print using .format():

>>> device = "clocks"
>>> time = "thirteen"
>>> sentence = "The {} were striking {}".format(device, time)
>>> print(sentence)
The clocks were striking thirteen
Enter fullscreen mode Exit fullscreen mode

Now, the same code, but using f-string:

>>> device = "clocks"
>>> time = "thirteen"
>>> sentence = f"The {device} were striking {time}"
>>> print(sentence)
The clocks were striking thirteen
Enter fullscreen mode Exit fullscreen mode

Looking at the two approaches it doesn't seem like much of a difference. But I think the improvement are enough to make me switch to f-strings and never look back.

  • F-strings save you eight characters by using a single 'f' to declare that the code uses f-strings, rather than the '.format()'.
  • Writing code is more intuitive, I feel, adding the string variables within the string itself, rather than saving this for the end of the line.
  • Reading code is easier because the declarations are right there in the string, again, rather than at the end of the line.
  • N.B. .format does allow variables in the braces, but that has the additional requirement of having to declare those in the final brackets, like so:
sentence = "The {device} were striking {time}".format(
device=device, time=time
)
Enter fullscreen mode Exit fullscreen mode

As you can see, this approach is even more verbose.

In isolation, all this points may seem minor, but as string formatting is such a necessary part of so much Python code, these small improvements can make a big difference to the overall experience of reading and writing Python.

Some extra details

Quotation marks can be used within an f-string, as long as different types are used

>>> f'{"quoted string"}'
'quoted string'
Enter fullscreen mode Exit fullscreen mode

Methods can be called directly

>>> sentence = f"The {device.upper()} were striking {time}"
>>> print(sentence)
The CLOCKS were striking thirteen
Enter fullscreen mode Exit fullscreen mode

For more details, check out PEP 498, the proposal that was developed into f-strings here.

Give f-strings a go, I hope you find them as useful as I have.

Thanks for reading.

🐦 @joeneville_

Top comments (7)

Collapse
 
evanroggenkamp profile image
Evan Roggenkamp

F strings are fantastic but format absolutely still has its uses. Named format placeholders allow dictionary expansion to dynamic arguments, for one, not to mention string formatting, alignment, etc. I agree for short concise strings, they’re excellent!

Collapse
 
joeneville_ profile image
Joe Neville

Good points, I didn't think about the alignment aspect as I was writing this. Thanks for the reminder.

Collapse
 
owluitar profile image
Matthew S. Emerson

I personally enjoy f-strings when using them to write formatted input files for different scientific analysis software.

Some softwares require input files with a specified column format, and others do not.
Having the ability to take an example input file, changing each parameter to an f-string that calls a variable stored in a dictionary in another file is very useful (especially for reproducibility in my case).

Collapse
 
chrisgreening profile image
Chris Greening

f-strings are one of my absolute favorite things in Python lol, gotta love expressive and concise code

Collapse
 
devzlx profile image
Zlx

true lol

Collapse
 
devzlx profile image
Zlx

To be honest, I like f strings because:

  1. They are compact
  2. Easier to remember
  3. Easier to understand

But I guess that if you want more uses then you can use formats

Collapse
 
joeneville_ profile image
Joe Neville

Good summary. I'm thinking along the same lines.