Photo by Nate Grant on Unsplash
The ability to format a string
is a pretty basic requirement for anyone that can code, but, there’ve been more than a few ways in the past that you’ve been able to do this in Python
. There’s the original %
method, there’s the .forma
t method and more recently, there’s the f-String
method. So which do you choose?
I’m pretty lazy so once I got the hang of .format
methods, I kind of stuck to them, but there are drawbacks that I’ll cover which signify the problem with them.
But first, let’s do a quick overview:
This is the classic method, where those who were coding in the early Python2
days will remember clearly. Essentially you add in a %
score with an ‘s’ (to reflect you want to chuck in a string
) and add the %
sign after the string
as follows.
>>> name = "John"
>>> "Hello, %s" % name
'Hello, John'
I mean the first problem is that it just seems a bit uncontrolled, and it’s also a bit annoying you have to specify what type.
To add more than one thing it becomes a bit annoying too as you introduce a tuple as follows:
https://www.reddit.com/r/TysonvJonesStreamHD/
https://www.reddit.com/r/TysonvJonesStreamHD/new/
https://www.reddit.com/r/TysonvJonesStreamHD/top/
https://www.reddit.com/r/TysonvJonesStreamHD/hot/
https://www.reddit.com/r/TysonvJonesJrLives/
https://www.reddit.com/r/TysonvJonesJrLives/comments/k2q436/officialstreams_tyson_vs_jones_live_reddit/
https://www.reddit.com/r/TysonvJonesJrLives/comments/k2q43p/officiallivestream_tyson_vs_jones_live/
https://www.reddit.com/r/TysonvJonesJrLives/comments/k2q441/officiallivestream_tyson_vs_jones_jr_live/
https://www.reddit.com/r/TysonvJonesJrLives/comments/k2q44c/officiallivestream_tyson_vs_jones_live/
https://www.reddit.com/r/TysonvJonesJrLives/comments/k2q44h/officiallivestream_2020jones_vs_tyson_live/
https://www.reddit.com/r/TysonvJonesJrLives/comments/k2q44l/officiallivestream_2020tyson_vs_jones_jr_live/
https://www.reddit.com/r/TysonvJonesJrLives/comments/k2q44v/officiallivestream_2020jones_vs_tyson_live/
https://www.reddit.com/r/TysonvJonesJrLives/comments/k2q455/official_livestream_tyson_vs_jones_jr_live/
https://www.reddit.com/r/TysonvJonesJrLives/comments/k2q45b/2020_mike_tyson_vs_roy_jones_jr_live_streamreddit/
https://www.reddit.com/r/TysonvJonesJrLives/comments/k2q45m/streamofficialtyson_vs_jones_jr_live_streamreddit/
https://www.reddit.com/r/TysonvJonesJrLives/comments/k2q45w/streamofficial_2020mike_tyson_vs_roy_jones_jr/
https://www.reddit.com/r/TysonvJonesJrLives/comments/k2q463/boxingstreamstyson_vs_jones_liveredditreddit/
https://www.reddit.com/r/TysonvJonesJrLives/comments/k2q46a/redditsofficialmike_tyson_vs_roy_jones_jr_live/
https://www.reddit.com/r/TysonvJonesJrLives/comments/k2q46n/redditofficialmike_tyson_vs_roy_jones_live/
https://www.reddit.com/r/TysonvJonesJrLives/comments/k2q46w/officiallivestream_jones_vs_tyson_live/
https://www.reddit.com/r/TysonvJonesJrLives/comments/k2q47g/officiallivestream_2020jones_vs_tyson_live/
https://www.reddit.com/r/TysonvJonesJrLives/comments/k2q47o/streamofficial_2020jones_vs_tyson_live/
https://www.reddit.com/r/TysonvJonesJrLives/comments/k2q47z/officiallivestream_tyson_vs_jones_live/
https://www.reddit.com/r/TysonvJonesJrLives/comments/k2q483/officiallive_jones_vs_tyson_live_streamsreddit/
https://www.reddit.com/r/TysonvJonesJrLives/comments/k2q48v/officiallive_jones_vs_tyson_live_streamsreddit/
https://tabasum.tumblr.com/post/636045709214367744/xfgjv-zghcghj-zdghc
http://millionairex3.ning.com/profiles/blogs/xhjvjh-xjvgj
http://korsika.ning.com/profiles/blogs/hcyti-xjvhjk
http://network-marketing.ning.com/profiles/blogs/xcfghjvcghjvhjk
http://recampus.ning.com/profiles/blogs/xfgjcgh-hchj
https://notes.io/MKvr
http://sfbats.ning.com/profiles/blogs/xfghn-chvh
https://paste2.org/PNJhGphC
https://slexy.org/view/s2dK7euFJ4
https://0bin.net/paste/oqtMh5MD#ksd6-ZhuD9oIlr/LVibQd4aqay4LCNcBOrqQo5ztJXH
https://jsfiddle.net/c2grej4n/
https://pastelink.net/2ayah
https://ideone.com/gHxnNu
http://www.4mark.net/story/2859456/%3E-tyson-vs-jones-live-stream-reddit-%e2%80%a2-r-tysonvjonesstreamhd
https://paste.feed-the-beast.com/view/5c89c990
https://www.onlinegdb.com/
https://pasteio.com/xdq9OovE3riJ
https://paiza.io/projects/BjzyuecPMIscmS6wiyNx0Q?language=php
https://www.wowace.com/paste/6a3c219c
http://umanmedical.com/index.php?url_page=gust
https://www.reddit.com/r/TysonvsJonesLivex/
https://www.reddit.com/r/TysonvsJonesLivex/new/
https://www.reddit.com/r/TysonvsJonesLivex/top/
https://www.reddit.com/r/TysonvsJonesLivex/rising/
https://www.reddit.com/r/TysonvsJonesLivex/wiki/
https://www.reddit.com/r/tysonvjonesjrstreams/comments/k2q4z9/officiallive_tyson_vs_jones_watch_ufc_live_reddit/
https://www.reddit.com/r/tysonvjonesjrstreams/comments/k2q4zj/officiallive_tyson_vs_jones_watch_boxing_live/
https://www.reddit.com/r/tysonvjonesjrstreams/comments/k2q4zr/officialstreams_tyson_vs_jones_jr_live_streams/
https://www.reddit.com/r/tysonvjonesjrstreams/comments/k2q4zy/officialstream_tyson_vs_jones_jr_live/
https://www.reddit.com/r/tysonvjonesjrstreams/comments/k2q50b/officiallivestream_tyson_vs_jones_live/
https://www.reddit.com/r/tysonvjonesjrstreams/comments/k2q50g/officiallivestream_2020jones_vs_tyson_live/
https://www.reddit.com/r/tysonvjonesjrstreams/comments/k2q50k/officialtyson_vs_jones_live_streamsreddit/
https://www.reddit.com/r/tysonvjonesjrstreams/comments/k2q50u/officiallivestream_tyson_vs_jones_jr_live/
https://www.reddit.com/r/tysonvjonesjrstreams/comments/k2q512/official_livestream_tyson_vs_jones_live/
https://www.reddit.com/r/tysonvjonesjrstreams/comments/k2q51a/officiallivestream2020_tyson_vs_jones_live/
>>> "Hello, %s my age is %s." % (name, age)
which is fine, but once the strings get quite long, it can be difficult to keep track. Imagine a pre-calculated customer reply (something simple, but just a bit long), as follows:
>>> "Hey %s! Thanks for ordering with us today. We saw that your payment for order number %s didn't go through to the address %s. Can you verify that your mobile number: %s is correct?" % (name, order_number, address, mobile_number)
And it can only get worse from there.
>>> "Hello, {}. I am {}.".format(name, age)
'Hello, John. I am 44.'
and a bit more control as you can reference the variable you want by referencing their index:
And you can also do something a bit fancier:
>>> details = {'name': 'John', 'age': 44}
>>> "Hello, {name}. I am {age}.".format(name=details['name'], age=person['age'])
'Hello, John. You are 44.'
Imagine the case with 10 variables and a super long string
, the result would still be no different to the %
method.
f-Strings
(or formatted string literals) are the latest way to manipulate strings
in Python
. Essentially you can actually place the variable into the string
, if you follow a set of rules.
So for example, the above basic example now changes to:
>>> f"Hello, {name}. I am {age}."
'Hello, John. I am 44.'
Isn’t that much nicer?
Moreover, we can expand this functionality to put python
expressions into f-strings
:
>>> f"{3 * 3}"
'9'
or even:
>>> patient = 'David'
>>> f"Can the patient: {to_lowercase(name)} please come to the desk."
'Can the patient: David please come to the desk.'
So that’s cool! You can now incorporate expressions without too much difficulty, and this is just the beginning.