String Formatting in Python

Sztring metódusokFeltételes utasítások

String formatting is about inserting values (like variables or calculations) into text in a clean and readable way. It's very useful when you want to create messages, reports, or display data to users.

Python has three main ways to do this. We'll start with the newest and easiest (f-strings), then look at the older methods for comparison.

f-strings (Formatted String Literals) – The Recommended Way

f-strings are the modern and preferred way since Python 3.6. You prefix the string with 'f' and put expressions inside curly braces {}. They are fast, readable, and allow direct variable insertion or even calculations.

str.format() Method

Before f-strings, str.format() was the standard way. You use {} placeholders and call .format() with values. It's flexible and good for when you need to reuse the same format with different data.

Old % Operator (Legacy Style)

This is the oldest way, similar to C's printf. It still works but is not recommended for new code because it's less readable and more error-prone.

Formatting Numbers

All methods allow precise number formatting, like fixed decimal places or adding commas for thousands.

Which Method to Use?

Use f-strings for new code – they're the fastest and easiest to read. Use str.format() when you need more complex formatting or compatibility with older Python versions. Avoid % formatting unless working with legacy code.

Quick Quiz

Az anyagokat átnéztük és ellenőriztük, de hibák továbbra is előfordulhatnak. A tartalom kizárólag oktatási célt szolgál, ezért saját felelősségre használd, és szükség esetén ellenőrizd más forrásokkal is.

✨ Kérdezd Larát — a tanulási partnered

Fedezd fel a személyre szabott tanulási támogatást. Lara elmagyarázza az anyagot, összefoglalja a témákat és megválaszolja a kérdéseidet — az Go csomagtól elérhető.


Lara segít gyorsabban tanulni — kizárólag a ReadyTools Go, Plus és Max tagoknak.

Kövesd nyomon a fejlődésed 🚀

Tanulj egyszerűbben utad nyomonkövetésével teljesen ingyen.