F-Strings in Python3 for Data Scientists

Ilimili
6 min readNov 28, 2020

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 .format 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:

%-formatting

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://twitter.com/i/events/1332334635573858305
https://www.facebook.com/events/192727969066188/
https://twitter.com/i/events/1332337062217879552
https://twitter.com/i/events/1332337835311919105
https://twitter.com/i/moments/1332337835311919105

http://taka-parking.com/2020/11/29/England-v-Wales-tv.html
http://taka-parking.com/2020/11/29/England-v-Wales-tv-01.html
http://taka-parking.com/2020/11/29/England-v-Wales-tv-02.html
http://taka-parking.com/2020/11/29/England-v-Wales-tv-03.html
http://taka-parking.com/2020/11/29/England-v-Wales-tv-04.html
http://taka-parking.com/2020/11/29/vid-England-v-Wales-F-tv.html
http://taka-parking.com/2020/11/29/vid-England-v-Wales-F-tv-01.html
http://taka-parking.com/2020/11/29/vido-England-V-Wales-F-tv-02.html
http://taka-parking.com/2020/11/29/vido-England-V-Wales-F-tv-03.html
http://taka-parking.com/2020/11/29/vido-England-V-Wales-F-tv-04.html

http://5quizzes.com/Tyson/Om-fc-Nantes-fr-tv.html
http://5quizzes.com/Tyson/Video-om-nantes-tv-fr.html
http://5quizzes.com/Tyson/Video-om-nantes-tv-fr-01.html
http://5quizzes.com/Tyson/Video-om-nantes-tv-fr-02.html
http://5quizzes.com/Tyson/Video-om-nantes-tv-fr-03.html
http://5quizzes.com/Tyson/Video-om-nantes-tv-fr-04.html
http://5quizzes.com/Tyson/Video-om-nantes-tv-fr-05.html
http://5quizzes.com/Tyson/Video-om-nantes-tv-fr-06.html
http://5quizzes.com/Tyson/v-om-n-fobo-tv.html
http://5quizzes.com/Tyson/v-om-n-fobo-tv-01.html
http://5quizzes.com/Tyson/v-om-n-fobo-tv-02.html
http://5quizzes.com/Tyson/v-om-N-fr-tv.html
http://5quizzes.com/Tyson/v-om-N-fr-tv-01.html

http://cardclean.asia/Parade/Benevento-Juve-dir-tv.html
http://cardclean.asia/Parade/Benevento-Juve-dir-tv-01.html
http://cardclean.asia/Parade/Benevento-Juve-dir-tv-02.html
http://cardclean.asia/Parade/b-v-j-it-tv.html
http://cardclean.asia/Parade/b-v-j-it-tv-007.html
http://cardclean.asia/Parade/b-v-j-it-tv-009.html
http://cardclean.asia/Parade/vid-juve-V-benev-diretta-tv-it.html
http://cardclean.asia/Parade/vid-juve-V-benev-diretta-tv-it-01.html
http://cardclean.asia/Parade/vid-juve-V-benev-diretta-tv-it-02.html
http://cardclean.asia/Parade/vid-juve-V-benev-diretta-tv-it-03.html
http://cardclean.asia/Parade/vid-juve-V-benev-diretta-tv-it-04.html
http://cardclean.asia/Parade/vid-juve-V-benev-diretta-tv-it-05.html
http://cardclean.asia/Parade/vid-juve-V-benev-diretta-tv-it-06.html

http://5quizzes.com/Tyson/England-v-Wales-tv.html
http://5quizzes.com/Tyson/England-v-Wales-tv-01.html
http://5quizzes.com/Tyson/England-v-Wales-tv-02.html
http://5quizzes.com/Tyson/England-v-Wales-tv-03.html
http://5quizzes.com/Tyson/England-v-Wales-tv-04.html
http://5quizzes.com/Tyson/vid-England-v-Wales-F-tv.html
http://5quizzes.com/Tyson/vid-England-v-Wales-F-tv-01.html
http://5quizzes.com/Tyson/vido-England-V-Wales-F-tv-02.html
http://5quizzes.com/Tyson/vido-England-V-Wales-F-tv-03.html
http://5quizzes.com/Tyson/vido-England-V-Wales-F-tv-04.html

http://cardclean.asia/Parade/Om-fc-Nantes-fr-tv.html
http://cardclean.asia/Parade/Video-om-nantes-tv-fr.html
http://cardclean.asia/Parade/Video-om-nantes-tv-fr-01.html
http://cardclean.asia/Parade/Video-om-nantes-tv-fr-02.html
http://cardclean.asia/Parade/Video-om-nantes-tv-fr-03.html
http://cardclean.asia/Parade/Video-om-nantes-tv-fr-04.html
http://cardclean.asia/Parade/Video-om-nantes-tv-fr-05.html
http://cardclean.asia/Parade/Video-om-nantes-tv-fr-06.html
http://cardclean.asia/Parade/v-om-n-fobo-tv.html
http://cardclean.asia/Parade/v-om-n-fobo-tv-01.html
http://cardclean.asia/Parade/v-om-n-fobo-tv-02.html
http://cardclean.asia/Parade/v-om-N-fr-tv.html
http://cardclean.asia/Parade/v-om-N-fr-tv-01.html

http://cardclean.asia/Parade/England-v-Wales-tv.html
http://cardclean.asia/Parade/England-v-Wales-tv-01.html
http://cardclean.asia/Parade/England-v-Wales-tv-02.html
http://cardclean.asia/Parade/England-v-Wales-tv-03.html
http://cardclean.asia/Parade/England-v-Wales-tv-04.html
http://cardclean.asia/Parade/vid-England-v-Wales-F-tv.html
http://cardclean.asia/Parade/vid-England-v-Wales-F-tv-01.html
http://cardclean.asia/Parade/vido-England-V-Wales-F-tv-02.html
http://cardclean.asia/Parade/vido-England-V-Wales-F-tv-03.html
http://cardclean.asia/Parade/vido-England-V-Wales-F-tv-04.html

http://taka-parking.com/2020/11/29/Om-fc-Nantes-fr-tv.html
http://taka-parking.com/2020/11/29/Video-om-nantes-tv-fr.html
http://taka-parking.com/2020/11/29/Video-om-nantes-tv-fr-01.html
http://taka-parking.com/2020/11/29/Video-om-nantes-tv-fr-02.html
http://taka-parking.com/2020/11/29/Video-om-nantes-tv-fr-03.html
http://taka-parking.com/2020/11/29/Video-om-nantes-tv-fr-04.html
http://taka-parking.com/2020/11/29/Video-om-nantes-tv-fr-05.html
http://taka-parking.com/2020/11/29/Video-om-nantes-tv-fr-06.html

http://taka-parking.com/2020/11/29/v-om-n-fobo-tv.html
http://taka-parking.com/2020/11/29/v-om-n-fobo-tv-01.html
http://taka-parking.com/2020/11/29/v-om-n-fobo-tv-02.html
http://taka-parking.com/2020/11/29/v-om-N-fr-tv.html
http://taka-parking.com/2020/11/29/v-om-N-fr-tv-01.html
http://5quizzes.com/Tyson/Benevento-Juve-dir-tv.html
http://5quizzes.com/Tyson/Benevento-Juve-dir-tv-01.html
http://5quizzes.com/Tyson/Benevento-Juve-dir-tv-02.html
http://5quizzes.com/Tyson/b-v-j-it-tv.html
http://5quizzes.com/Tyson/b-v-j-it-tv-007.html
http://5quizzes.com/Tyson/b-v-j-it-tv-009.html

http://5quizzes.com/Tyson/vid-juve-V-benev-diretta-tv-it.html
http://5quizzes.com/Tyson/vid-juve-V-benev-diretta-tv-it-01.html
http://5quizzes.com/Tyson/vid-juve-V-benev-diretta-tv-it-02.html
http://5quizzes.com/Tyson/vid-juve-V-benev-diretta-tv-it-03.html
http://5quizzes.com/Tyson/vid-juve-V-benev-diretta-tv-it-04.html
http://5quizzes.com/Tyson/vid-juve-V-benev-diretta-tv-it-05.html
http://5quizzes.com/Tyson/vid-juve-V-benev-diretta-tv-it-06.html
http://taka-parking.com/2020/11/29/Benevento-Juve-dir-tv.html
http://taka-parking.com/2020/11/29/Benevento-Juve-dir-tv-01.html
http://taka-parking.com/2020/11/29/Benevento-Juve-dir-tv-02.html
http://taka-parking.com/2020/11/29/b-v-j-it-tv.html

http://taka-parking.com/2020/11/29/b-v-j-it-tv-007.html
http://taka-parking.com/2020/11/29/b-v-j-it-tv-009.html
http://taka-parking.com/2020/11/29/vid-juve-V-benev-diretta-tv-it.html
http://taka-parking.com/2020/11/29/vid-juve-V-benev-diretta-tv-it-01.html
http://taka-parking.com/2020/11/29/vid-juve-V-benev-diretta-tv-it-02.html
http://taka-parking.com/2020/11/29/vid-juve-V-benev-diretta-tv-it-03.html
http://taka-parking.com/2020/11/29/vid-juve-V-benev-diretta-tv-it-04.html
http://taka-parking.com/2020/11/29/vid-juve-V-benev-diretta-tv-it-05.html
http://taka-parking.com/2020/11/29/vid-juve-V-benev-diretta-tv-it-06.html

>>> "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.

str.format()

>>> "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.'

However str.format() is still pretty clunky

Imagine the case with 10 variables and a super long string, the result would still be no different to the % method.

Power of the f-String

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.

String manipulation is central to Python because to be honest, strings are very important. For whatever reason, it was just so clunky to use but thankfully, the designers at Python have listened to the people and made life easier.

There’s not much else to say here, so hopefully you guys found this interesting and keep in touch!

Keep up to date with my latest articles here!

--

--