Marky avatar

[Python Tips] f-strings in Python 3.6 and why you should be using them

themarkymark

Published: 01 Apr 2018 › Updated: 01 Apr 2018[Python Tips] f-strings in Python 3.6 and why you should be using them

[Python Tips] f-strings in Python 3.6 and why you should be using them

image.png

What are f-strings and why are they so awesome?

f-strings is a new streamlined way of formatting strings that came with Python 3.6. Traditionally you would format a string like this:

employee = 'Bob'
role = 'Manager'
company = 'Amazon'
print('{} is a {} at {}.'.format(employee, role, company))

With f-strings you can do the same this with the following code:

employee = 'Bob'
role = 'Manager'
company = 'Amazon'
print(f'{employee} is a {role} at {company}')

Expressions in f-string

You can use python expressions inside of your curly braces.

name = 'Dog Dog'
age = 4
print(f'{name} is {age * 7} in dog years')

Performance considerations

f-strings are fast, twice as fast as format() as they are stored as bytecode.

Why you should vote me as witness

Witness & Administrator of four full nodes

X48EJ

themarkymark.png


My recent popular posts

STEEM, STEEM Power, Vests, and Steem Dollars. wtf is this shit?
The truth and lies about 25% curation, why what you know is FAKE NEWS
WTF is a hardware wallet, and why should you have one?
GINABOT - The Secret to your Sanity on Steemit
How to calculate post rewards
Use SSH all the time? Time for a big boy SSH Client
How to change your recovery account
How curation rewards work and how to be a kick ass curator
Markdown 101 - How to make kick ass posts on Steemit
Work ON your business, not in your business! - How to succeed as a small business
You are not entitled to an audience, you need to earn it!
How to properly setup SSH Key Authentication - If you are logging into your server with root, you are doing it wrong!
Building a Portable Game Console

alt

Leave [Python Tips] f-strings in Python 3.6 and why you should be using them to:

Written by

Browncoat | Meme Connoisseur | Bitcoin Evangelist | Dev | Gamer | Technical Samurai | AI Nerd | Hive Witness | Black Belt in Dad Jokes

Read more #programming posts


Best Posts From Marky

We have not curated any of themarkymark's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Marky