Tuesday Tip #2: Python f-strings? f-yeah! 🧶


Hi Reader!

In case you missed last week's announcement, I'll be sharing a new data science tip with you every Tuesday!


👉 Tip #2: How to use f-strings in Python

Python introduced f-strings back in version 3.6 (six years ago!), but I've only recently realized how useful they can be.

Let's start with some simple examples of how they're commonly used, and then I'll end with a real-world example (using pandas).

Substituting objects:

To make an f-string, you simply put an "f" in front of a string. By putting the "name" and "age" objects inside of curly braces, those objects are automatically substituted into the string.

Calling methods and functions:

Strings have an upper() method, and so I was able to call that method on the "role" string from within the f-string.

Evaluating expressions:

You can evaluate an expression (a math expression, in this case) within an f-string.

Formatting numbers:

This looks much nicer, right? The colon begins the format specification, and the ".1%" means "format as a percentage with 1 digit after the decimal point."

Further reading:

🔗 Guide to f-strings (written by my pal Trey Hunner)

🔗 f-string cheat sheet (also by Trey)

Real-world example using pandas:

Recently, I was analyzing the survey data submitted by 500+ Data School subscribers. I asked each person about their level of experience with 11 different data science topics/tools, plus their level of interest in improving those skills this year.

Thus I had 22 columns of data, with names like “python_experience”, “python_interest”, “pandas_experience”, “pandas_interest”, etc.

Each “experience” column was coded from 0 (None) to 3 (Advanced), and each “interest” column was coded from 0 (Not interested) to 2 (Definitely interested).

Among other things, I wanted to know the mean level of interest in each topic, as well as the mean level of interest in each topic by experience level.

Here's what I did to answer those questions:

(The “cats” list actually had 11 categories, so the loop allowed me to examine all of the categories at once.)

Notice how I used f-strings:

🧵 Because of the naming convention, I could access the DataFrame columns using df[f'{cat}_interest'] and df[f'{cat}_experience']

🧵 I capitalized the category using f'{cat.upper()}' to help it stand out

🧵 I formatted the mean interest to 2 decimal places using f'{mean_interest:.2f}'

How helpful was today’s tip?

🤩🙂😐


Do you have a favorite use for f-strings? Click reply and let me know!

See you next Tuesday!

- Kevin

P.S. Did someone awesome forward you this email? Sign up here to receive data science tips every week!

Learn Artificial Intelligence from Data School 🤖

Join 25,000+ intelligent readers and receive AI tips every Tuesday!

Read more from Learn Artificial Intelligence from Data School 🤖

Hi Reader, This week, I've got a short tip about AI agents, followed by some Data School news... 👉 Tip #56: What are AI agents? Google is calling 2025 "the agentic era," DeepLearning.AI says "the agentic era is upon us," and NVIDIA's founder says "one of the most important things happening in the world of enterprise is agentic AI." Clearly AI agents are a big deal, but what exactly are they? Simply put, an AI agent is an application that uses a Large Language Model (LLM) to control its...

Hi Reader, Last week, I launched a brand new course: Build an AI chatbot with Python. 120+ people enrolled, and a few have already completed the course! 👏 Want to join us for $9? 👉 Tip #55: Should you still learn to code in 2025? You’ve probably heard that Large Language Models (LLMs) are excellent at writing code: They are competitive with the best human coders. They can create a full web application from a single prompt. LLM-powered tools like Cursor and Copilot can autocomplete or even...

Hi Reader, The Python 14-Day Challenge starts tomorrow! Hope to see you there 🤞 👉 Tuesday Tip: My top 5 sources for keeping up with AI I'll state the obvious: AI is moving incredibly FAST 💨 Here are the best sources I follow to keep up with the most important developments in Artificial Intelligence: The Neuron (daily newsletter) My top recommendation for a general audience. It’s fun, informative, and well-written. It includes links to the latest AI news and tools, but the real goldmine is...