3 Tips for Advancing Python String Formatting
Start using f-string to align code and make strings readable, clean, and explicit
String formatting means embedding expressions inside string literals. In most of the Python tutorials for beginners, the first task starts with the “Hello World” project, which makes
string the most familiar variable type.
However, once you start adding parameters inside a longer string, your code would be less concise and readable. For example:
Obviously, the string concatenation looks bad and wordy, so the Python Software Foundation invented a function called
.format() to shorten the syntax:
It’s more intuitive but still not easily readable, so why not adopt new ways?
By the end of this article, you will learn three ways to keep your code concise:
- Formatted string literals.
- Output alignment.
- Maximum line length.
What Are Formatted String Literals?
Formatted string literals (f-string) feature a simple syntax and shorter expressions, which replace “old school” methods like %-formatting or
Even better, it executes faster than these old methods, and the clue is provided in PEP 498:
“F-strings provide a way to embed expressions inside string literals, using a minimal syntax. It should be noted that an f-string is really an expression evaluated at run time, not a constant value.”
In short, f-string uses fewer bytecode instructions than other methods. If you are interested in the advanced interpretation of f-string, check out the Python docs.
To show how f-strings work. Let’s reuse the example above:
The string has an
f at the beginning and curly braces involving expressions replaced with their values. This technique can also cooperate with dictionary/class/list.
Furthermore, f-string supports various format specifications to eliminate excessive work, while the syntax remains concise.
How to Implement Output Alignment
How much time have you spent on counting digits of numbers to distinguish the exact value?
In the real world, we are unconsciously affected by how the texts are formatted and expect nice alignment for a comfortable reading experience. In Python’s output display, we have powerful tools for doing so:
- Commas “
,” to separate groups of thousands.
2. Limited digits to display lengthy numbers.
3. Space padding to align the paragraphs.
These examples are the commonly used specifications, and other options can be found in Python’s official docs, including showing positive or negative signs, changing positional systems, and displaying percentage signs.
Because the notations are too massive to remember, using the cheat sheet is always the right choice.
Assuming that the paragraph about f-strings isn’t enough for you, try this one!
(I’ve always been a big fan of Real Python!)
Why Is Maximum Line Length Important?
Don’t stress the reader by sticking too much code in one line. A recommended maximum line length is 79 characters according to the Style Guide for Python Code.
The limitation of the editor window width works well when using code review tools. Whenever you want to copy codes from one file to another or use Git to compare two code versions, the controlled window length makes it possible to have several files open side-by-side.
The ability to simplify means to eliminate the unnecessary so that the necessary may speak. String formatting won’t cause any effect on your algorithm, but it is gentle to make others comfortable while reading your code.
For more tips to make code nice and tidy:
How to Make Your Python Code More Elegant
My interpretation of the Zen of Python and more tips to make code nice and tidy
Thanks for reading! I hope you enjoyed it.