F is for f-strings - Python A to Z Dark _ □ ✕
  • Posts
  • About
  • Debug
Post

F is for f-strings - Python A to Z

Published
2026-08-07 00:00 UTC (precision: second)
Source feed
Juha-Matti Santala - Community Builder. Dreamer. Adventurer. excluded
Link
https://hamatti.org/posts/f-is-for-f-strings-python-a-to-z/
Canonical
https://hamatti.org/posts/f-is-for-f-strings-python-a-to-z
Length
2966 runes
Extracted text
Python A-Z is a blog series about Python. Each day, I share insights, ideas and examples for different parts of Python development that match with the letter of the day. Blaugust is an annual blogging festival in August where the goal is to write a blog post every day of the month. Programming languages have many ways to combine literal strings and variables. For a decade now, we in Python land have been able to enjoy the beauty and elegance of f-strings. f-string is a string that is prefixed with the letter f: string_type = 'f-string' print(f'This is a {string_type}') When you craft an f-string, you can evaluate Python code inside it by placing it inside curly braces `{ … }`. In addition to simply putting in a variable, there are a bunch of format specifications that allow further modification of the output. fstring.help has a really handy reference sheet for these but here are some that I use regularly. Padding numbers File browsers usually default to sorting alphabetically so if you have files 1.json, 2.json and 10.json, the 10 will sort before the 2. So to keep things neat and tidy, I often left-pad with zeroes to force proper sorting: for filename in range(1, 20): print(f'{filename:02d}') # Prints 01, 02, 03, ... 20 Here 0 is the value we use to fill and 2 is the width to how many characters we want to pad up to. Aligning text When I build command line tools, I want a well-defined output structure and aligning items left or right with a given width is helpful. name, goals = 'Juhis', 20 print(f'{name:<10}{goals:>5}') # 'Juhis 20' < is for left-align, > is for right-align, ^ would be for center and the number is the width. In this example, it guarantees that the name field is at least 10 characters and goals field is 5. You can combine this with the previous example if you want to fill the empty space with something else, like underscore: item, value = 'Cucumber', 14 print(f'{item:_<10}{value:_>5}') # Cucumber_____14 Debugging For debugging, I use two formats. (Btw, if you wanna get better at debugging, check out Debugging Python and if you’re curious about why printing is a good debug tool, read print is your best debugging tool.) The first one uses = sign to print out the name of the variable and its value separated with an equal sign. It’s a short hand for f'value={value}' . unknown = 400 print(f'{unknown=}, {unknown = }') # Whitespace is preserved # unknown=400, unknown = 400 The second one uses !r to print out object’s __repr__ value. class Card: def __init__(self, name, number): self.name = name self.number = number def __repr__(self): return f'Card("{self.name}", {self.number})' card = Card('Pikachu', 25) print(f'{card!r}') # Card("Pikachu", 25) Percentages The last one is actually something I didn’t know about before I started writing this but can be handy. You can convert floats to percentages with :.0%: percent = 0.3738 print(f'{percent:.0%}') # 37% print(f'{percent:.2%}') # 37.38% It’s pretty handy!
Stored topics

Every stored assignment is listed, including rows below the current cutoff (0.75), so a missing tag can be explained.

Concept Code Score Meter Margin Origin Path
economy, business and finance economy-business-and-finance 0.98 4.17 ↑derived from local:software-development economy, business and finance
products and services products-and-services 0.98 4.17 ↑derived from local:software-development economy, business and finance > products and services
computing and information technology computing-and-information-technology 0.98 4.17 ↑derived from local:software-development economy, business and finance > products and services > computing and information technology
software and applications software-and-applications 0.98 4.17 ↑derived from local:software-development economy, business and finance > products and services > computing and information technology > software and applications
Software development software-development 0.98 4.17 direct economy, business and finance > products and services > computing and information technology > software and applications > Software development
science and technology science-and-technology 0.98 3.87 direct science and technology
technology and engineering technology-and-engineering 0.96 3.28 direct science and technology > technology and engineering
information technology and computer science information-technology-and-computer 0.93 2.60 direct science and technology > technology and engineering > information technology and computer science
arts, culture, entertainment and media arts-culture-entertainment-and-media 0.84 1.67 ↑derived from local:blogging arts, culture, entertainment and media
mass media mass-media 0.84 1.67 ↑derived from local:blogging arts, culture, entertainment and media > mass media
social media social-media 0.84 1.67 ↑derived from local:blogging arts, culture, entertainment and media > mass media > social media
Blogging blogging 0.84 1.67 direct arts, culture, entertainment and media > mass media > social media > Blogging
Open source open-source 0.47 -0.10 direct economy, business and finance > products and services > computing and information technology > software and applications > Open source
artificial intelligence artificial-intelligence 0.39 -0.45 direct science and technology > technology and engineering > information technology and computer science > artificial intelligence
Information security information-security 0.29 -0.91 direct science and technology > technology and engineering > information technology and computer science > Information security
arts and entertainment arts-and-entertainment 0.16 -1.67 ↑derived from 20000013 arts, culture, entertainment and media > arts and entertainment
literature literature 0.16 -1.67 direct arts, culture, entertainment and media > arts and entertainment > literature
society society 0.12 -1.96 ↑derived from 20001300 society
fundamental rights fundamental-rights 0.12 -1.96 ↑derived from 20001300 society > fundamental rights
privacy privacy 0.12 -1.96 direct society > fundamental rights > privacy
education education 0.07 -2.56 direct education
media and entertainment industry media-and-entertainment-industry 0.07 -2.59 ↑derived from 20000306 economy, business and finance > products and services > media and entertainment industry
books and publishing books-and-publishing 0.07 -2.59 direct economy, business and finance > products and services > media and entertainment industry > books and publishing
music music 0.06 -2.73 direct arts, culture, entertainment and media > arts and entertainment > music
scientific research scientific-research 0.06 -2.73 ↑derived from 20000739 science and technology > scientific research
scientific exploration scientific-exploration 0.06 -2.73 ↑derived from 20000739 science and technology > scientific research > scientific exploration
space exploration space-exploration 0.06 -2.73 direct science and technology > scientific research > scientific exploration > space exploration
health health 0.06 -2.80 ↑derived from 20000464 health
health treatment and procedure health-treatment-and-procedure 0.06 -2.80 direct health > health treatment and procedure
social sciences social-sciences 0.05 -2.86 direct science and technology > social sciences
lifestyle and leisure lifestyle-and-leisure 0.05 -2.88 ↑derived from 20000550 lifestyle and leisure
leisure leisure 0.05 -2.88 ↑derived from 20000550 lifestyle and leisure > leisure
hobby hobby 0.05 -2.88 direct lifestyle and leisure > leisure > hobby
game game 0.05 -2.97 ↑derived from local:mmorpg lifestyle and leisure > leisure > game
video game video-game 0.05 -2.97 ↑derived from local:mmorpg lifestyle and leisure > leisure > game > video game
MMORPG mmorpg 0.05 -2.97 direct lifestyle and leisure > leisure > game > video game > MMORPG
politics and government politics-and-government 0.05 -3.04 direct politics and government
Tabletop gaming tabletop-gaming 0.04 -3.09 direct lifestyle and leisure > leisure > game > Tabletop gaming
crime, law and justice crime-law-and-justice 0.04 -3.12 ↑derived from 20000086 crime, law and justice
crime crime 0.04 -3.12 ↑derived from 20000086 crime, law and justice > crime
cyber crime cyber-crime 0.04 -3.12 direct crime, law and justice > crime > cyber crime
sport sport 0.04 -3.13 direct sport
competition discipline competition-discipline 0.04 -3.26 ↑derived from 20001183 sport > competition discipline
eSports esports 0.04 -3.26 direct sport > competition discipline > eSports
human interest human-interest 0.04 -3.27 ↑derived from 20000498 human interest
award and prize award-and-prize 0.04 -3.27 direct human interest > award and prize
exercise and fitness exercise-and-fitness 0.04 -3.27 direct lifestyle and leisure > wellness > exercise and fitness
wellness wellness 0.04 -3.27 ↑derived from 20001239 lifestyle and leisure > wellness
film industry film-industry 0.04 -3.29 direct economy, business and finance > products and services > media and entertainment industry > film industry
environment environment 0.03 -3.43 ↑derived from 20000418 environment
climate change climate-change 0.03 -3.43 direct environment > climate change
natural science natural-science 0.03 -3.45 direct science and technology > natural science
biology biology 0.03 -3.48 direct science and technology > natural science > biology
Self-hosting self-hosting 0.03 -3.52 direct economy, business and finance > products and services > computing and information technology > Self-hosting
animation animation 0.02 -3.70 direct arts, culture, entertainment and media > arts and entertainment > animation
diet diet 0.02 -3.81 direct health > health treatment and procedure > diet
Cryptography cryptography 0.02 -3.86 direct science and technology > technology and engineering > information technology and computer science > Cryptography
conflict, war and peace conflict-war-and-peace 0.02 -4.04 direct conflict, war and peace
labour labour 0.02 -4.10 direct labour
public health public-health 0.02 -4.10 direct health > public health
lifestyle lifestyle 0.02 -4.16 ↑derived from 20001257 lifestyle and leisure > lifestyle
house and home house-and-home 0.02 -4.16 ↑derived from 20001257 lifestyle and leisure > lifestyle > house and home
gardening gardening 0.02 -4.16 direct lifestyle and leisure > lifestyle > house and home > gardening
family family 0.01 -4.21 direct society > family
history history 0.01 -4.38 direct science and technology > social sciences > history
disease and condition disease-and-condition 0.01 -4.40 ↑derived from 20000458 health > disease and condition
mental health and disorder mental-health-and-disorder 0.01 -4.40 direct health > disease and condition > mental health and disorder
visual arts visual-arts 0.01 -4.40 ↑derived from 20000036 arts, culture, entertainment and media > arts and entertainment > visual arts
photography photography 0.01 -4.40 direct arts, culture, entertainment and media > arts and entertainment > visual arts > photography
culture culture 0.01 -4.45 direct arts, culture, entertainment and media > culture
weather weather 0.01 -4.48 ↑derived from 20001128 weather
weather forecast weather-forecast 0.01 -4.48 direct weather > weather forecast
consumer goods consumer-goods 0.01 -4.48 ↑derived from 20001160 economy, business and finance > products and services > consumer goods
handicrafts handicrafts 0.01 -4.48 direct economy, business and finance > products and services > consumer goods > handicrafts
television television 0.01 -4.56 direct arts, culture, entertainment and media > mass media > television
disaster, accident and emergency incident disaster-accident-and-emergency-incident 0.01 -4.62 direct disaster, accident and emergency incident
travel and tourism travel-and-tourism 0.01 -4.70 direct lifestyle and leisure > leisure > travel and tourism
streaming service streaming-service 0.01 -4.81 direct economy, business and finance > products and services > media and entertainment industry > streaming service
card game card-game 0.01 -4.82 direct lifestyle and leisure > leisure > game > card game
board game board-game 0.01 -5.06 direct lifestyle and leisure > leisure > game > board game
podcast podcast 0.00 -5.40 direct economy, business and finance > products and services > media and entertainment industry > podcast
food and drink enthusiasm food-and-drink-enthusiasm 0.00 -5.55 direct lifestyle and leisure > leisure > hobby > food and drink enthusiasm
religion religion 0.00 -5.60 direct religion

← Back to posts

feedtagger 0.1.0-dev model: modernbert-base-zeroshot-v2.0/int8@all-s256 651 posts 0 unclassified min score 0.75