07 How to Use String Format in Python + Tips (Video in Khmer)
Learn Python string format with f-string, format() and % with simple example and tips. Short and clear. Good for beginner!
2026-10-20 ยท 4 min read
Hello friend! ๐ You want put variable inside text? Use string format!
In this post, I show you string format in Python. Easy and short. ๐
๐ค Why String Format?
Join text with + is hard to read:
name = "Dara"
age = 20
print("My name is " + name + " and I am " + str(age) + " years old.")
String format is cleaner. No need str().
print(f"My name is {name} and I am {age} years old.")
Python have 3 way to format string:
| Way | Example |
|---|---|
| f-string | f"Hi {name}" |
format() | "Hi {}".format(name) |
% | "Hi %s" % name |
Use f-string. It is newest and best.
โจ Way 1: f-string
Put f before the quote. Put variable inside { }.
name = "Dara"
price = 2.5
qty = 3
print(f"Hello {name}") # Hello Dara
print(f"Total: {price * qty}") # Total: 7.5
print(f"Upper: {name.upper()}") # Upper: DARA
You can run code inside { }.
๐ฅ Video: String Formatting in Python Part 1 (Khmer)
๐งฉ Way 2: format()
Use {} as empty place. Then put value in format().
print("Hello {}".format("Dara")) # Hello Dara
print("{} is {} years old".format("Dara", 20)) # Dara is 20 years old
print("{1} {0}".format("World", "Hello")) # Hello World (by position)
print("{name} is {age}".format(name="Sok", age=25)) # Sok is 25
๐ฃ Way 3: % Operator (Old Style)
You may see this in old code.
name = "Dara"
age = 20
print("Hello %s" % name) # %s = string
print("I am %d years old" % age) # %d = integer
print("Price: %.2f" % 3.14159) # %f = float -> 3.14
print("%s is %d years old" % (name, age)) # many value use ( )
๐จ Format Number
This is part 2. Control how number look.
pi = 3.14159265
big = 1234567
rate = 0.256
print(f"{pi:.2f}") # 3.14 2 digit after dot
print(f"{big:,}") # 1,234,567 add comma
print(f"{rate:.1%}") # 25.6% percent
print(f"{5:03}") # 005 fill 0, total 3 digit
print(f"{255:b}") # 11111111 binary
๐ Align Text
print(f"|{'left':<10}|") # |left |
print(f"|{'right':>10}|") # | right|
print(f"|{'center':^10}|") # | center |
print(f"|{'hi':*^10}|") # |****hi****| fill with *
<left>right^center
๐ Example: Simple Table
items = [("Apple", 1.5), ("Banana", 0.75), ("Orange", 2)]
for name, price in items:
print(f"{name:<10} ${price:>6.2f}")
Output:
Apple $ 1.50
Banana $ 0.75
Orange $ 2.00
๐ฅ Video: String Formatting in Python Part 2 (Khmer)
โ Common Error
Forget the f:
name = "Dara"
print("Hello {name}")
Output: Hello {name}. Python print it as normal text. Fix: add f.
print(f"Hello {name}")
Same quote inside f-string (older Python):
print(f"Name: {user["name"]}") # error in Python before 3.12
print(f"Name: {user['name']}") # good
Need show { } as text? Use double:
print(f"Use {{name}} like this") # Use {name} like this
๐ก Tips
- Use f-string first. Short, fast and easy to read.
- Use
:.2ffor money or price. Always 2 digit after dot. - Use
:,for big number. Easy to read:1,000,000. - Use
<,>,^to make nice table in terminal. - Use
{value = }for quick debug:f"{age = }"โage = 20 - Don't forget the
fbefore quote! - Use
format()when text come from variable or file (template). - Use
%only when you read old code.
โ Any Question?
Tell us in comment below! ๐
#Python #StringFormat #fstring #LearnPython #Beginner