Rodrigo πŸπŸš€ Profile picture
Nov 21, 2021 β€’ 10 tweets β€’ 4 min read β€’ Read on X
I have been tweeting about Python 🐍 string formatting.

I have been preaching πŸ™ that f-strings are the best string formatting alternative.

Then comes the string method `.format`.

And only then, %-formatting.

Here is a thread 🧡 comparing the 3 πŸ‘‡
In its most basic form,

πŸ‘‰ %-formatting uses % and a letter inside the string
πŸ‘‰ `.format` replaces sequences of {} with the data
πŸ‘‰ f-strings use {} to insert the data _inside_ the string

Here is how it looks like πŸ‘‡
An undervalued feature of string formatting is that you can easily determine whether your data should be formatted with its string (str) or with its representation (repr).

For debugging, `repr` is usually more useful.

Here is how this looks like πŸ‘‡
Aligning the data inside a field is also much easier with `.format` and f-strings.

For one, %-formatting can't handle centre alignment.

Secondly, with `.format` and f-strings you just use <^>, which points to where the alignment should be!

Easy to remember πŸ€”πŸ’­βœ…
Now, to be fair, there are some situations when I think `.format` is better than f-strings.

Such an example is when we have the formatting data inside a dictionary, with string keys.

In that case, `.format` is great!
There is another situation in which `.format` and f-strings completely blow %-formatting out of the water.

With `.format` and f-strings, we can implement custom formatting specifiers for our own objects.

All we need is `__format__`!

Here is a (silly) demo πŸ‘‡
These were just some comparison points between the three main ways of doing string formatting in Python.

I took these examples from a Pydon'ts article of mine, that you can read here πŸ‘‡

mathspp.com/blog/pydonts/s…
I'm currently working on a string formatting case study.

What I mean is, I will take an old Python project that is used in real life...

But that uses only %-formatting.

And I will review the whole thing, upgrading the string formatting as I go.
Then, I will write a full article explaining

πŸ‘‰ what I did
πŸ‘‰ where I decided to use f-strings and where I decided to use `.format`
πŸ‘‰ why I made those decisions

Sounds interesting?

Then subscribe to my newsletter to stay in the loop πŸ˜‰

mathspp.com/subscribe
TL;DR:

πŸ‘‰ f-strings are generally better than `.format`:
shorter & more expressive

πŸ‘‰ %-formatting should be avoided:
old & less features

πŸ‘‰ do modern alignment with <^>

πŸ‘‰ .format is nicer than f-strings when data is in a dictionary with string keys

See you around πŸ‘‹

β€’ β€’ β€’

Missing some Tweet in this thread? You can try to force a refresh
γ€€

Keep Current with Rodrigo πŸπŸš€

Rodrigo πŸπŸš€ Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @mathsppblog

May 18, 2023
I know `print` is the first Python 🐍 function you learned! πŸš€

And yet, you don't know this about `print` πŸ‘‡ Image
What you know for sure is that `print` will take an object and it will print it on the screen.

That's the basic functionality it provides: Image
Maybe you don't know that `print` can actually print multiple things!

If you pass multiple arguments, they all get printed: Image
Read 11 tweets
May 17, 2023
I'll tell you the story of a person that had the wrong name…

And how to prevent that in Python 🐍 with properties πŸš€.

πŸ‘‡ Image
John Doe was a regular guy and when he was born, he was inserted into the government's database of people.

They created a new `Person` and added John's details: Image
John never liked his name Doe, though.

So Joe decided to change his name to Smith.

And so he did.

He updated his last name, but the government `Person` STILL had the wrong name! Image
Read 10 tweets
May 14, 2023
Opening a file to read/write is a common task in Python 🐍.

Here is how to do it right! πŸš€

πŸ‘‡ Image
Python has a built-in `open` that takes a file path and opens that file.

Then, you have to specify whether you want to open the file to read, write, or append.

But this isn't half of the story! Image
The default behaviour is to open the file to read/write text.

This works well with TXT or CSV files, for instance.

If you need to open a file to read its binary contents, you can add a `"b"` to the mode: Image
Read 6 tweets
May 13, 2023
The Python 🐍 built-in `round` is great. πŸš€

Here are some tips on it. πŸ‘‡ Image
The purpose of `round` is to… round numbers!

It rounds numbers to the closest integer.

These are some simple examples: Image
However, if the number ends in `.5`, what is the closest integer?

In that case, `round` will choose the even number.

This means it may round up or down πŸ€ͺ

(In school, I was taught to round `.5` up… 🀷) Image
Read 6 tweets
May 12, 2023
Error handling in Python 🐍 made simple. πŸš€

πŸ‘‡ Image
The keyword `try` is used before code that might fail.

So, if you know something can raise an error, you can write it inside a `try` statement: Image
Now that the code is inside a `try` statement, you need to tell Python what error you want to handle, and how.

That's when the keyword `except` comes in! Image
Read 7 tweets
May 11, 2023
Every Python 🐍 programmer uses the ICPO rule. πŸš€

Even if you don't know what it is.

Let me tell you what the ICPO rule is and what it does πŸ‘‡ Image
The ICPO rule is what determines how attribute and method lookup work.

More precisely, it is the ICPO rule that tells Python where to look for attributes and methods in your objects.

This is relevant because everything in Python is an object.
Because everything is an object, that means Python is incredibly consistent in some things.

For example, did you know that everything has a type? Image
Read 13 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(