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
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
Cool I’ll try that out with “”” strings which have been coming in handy a lot lately
This is a really good tip, I am just starting to learn some Python. F-strings are going to save me so much time 😬
These remind me of those
`Javascript multiline ${thingies}`
which are pretty neat. It's nice to see that they are also in Python. Another reason to give it a try, since I've been neglecting it for a while in favour of JS.
a very great post I really like a friend. please help me friend.
Very interesting, very useful ..
Something very useful ..
thank you nice blog nice post sharing.
I have no idea what I just read lol but I'll let Frank (@fstmaurice) know about f-strings. He writes code, so he'll understand this language. In my mind, I'm thinking F for Frank, f-strings, fffffff, and then my mind went to farts.
Thank you, I try to support specific code examples and technique advice whenever I have voting power to offer. Keep them coming, I will have to give this method of string formatting a try, adding error handling of sorts to the example would be of value, as during runtime I presume errors are likely.
Why should I be using f strings?
Next time it would be cool if you could show the examples and resulting ouput in the Python 2.7 and 3.6 interpreter shells. Thanks for quick read, I work with Python so this is interesting.
I don’t touch 2.7.
The post is pretty clear of the two advantages.
I've just read two of your python posts and learnt something new in both of them! I never even realized you were a programmer! Anyway, thanks for this, I've set a follow on you
I’m not, but I do code. :)
It should be called "string interpolation". Ruby has it, too.
Ha that's awesome, I've been using python 2.6/2.7 mostly due to the version that graphics applications use. So hadn't known about this in python 3.6, that's just lovely :D
A lot of people still use 2.7 but 3 is the way to go. Most everything has been moved over now. I know Google still uses 2.7 a lot but they have a ton of custom systems they need to migrate.