Sure, sex is cool, but have you ever written efficient code that worked on the first try? F-strings are a great tool for writing clean, concise code. If you’re new to Python, or just looking for a quick refresher to clean up your code, save yourself some time and learn about f-strings.

What’s an f-string?

An f-string is a type of formatted Python string literal. It’s easily the most user-friendly Python feature I’ve encountered, and was a great confidence booster as a new programmer.

Here’s the basic format of an f-string:

print(f “[string] {placeholder1} {placeholder2}”)

It doesn’t look like much here, but it’s super helpful when working with numbers and multiple variables in a string.

Example program with an f-string:

Let’s say that you want to display a message that greets the user by name, and tells them how much they have left to spend from their budget that month.

◆ What our code will display at the end: “ Hi {user’s first name}! You’ve spent {x} dollars this month, and have {x} dollars left to spend.”

◆ What we need to get from the user: Their first name, and the number of US dollars they spent this month

◆ What we need to calculate: How many US dollars remain from the user’s $500 monthly budget

#coding #beginners-guide #python

Cleaning Up Your Code With F-Strings in Python
2.90 GEEK