Python String Formatting


Python provides several ways to format strings — combining text with variables or data, while keeping the output readable and clean.


🔹 The format() Method

You can insert variables into strings using {} placeholders and .format().

name = "Alice"
age = 25

print("My name is {} and I am {} years old.".format(name, age))

✅ Output: My name is Alice and I am 25 years old.


🔹 Positional and Keyword Arguments

You can specify the order using numbers or names.

print("Hello {0}, your score is {1}".format("Bob", 90))
print("Name: {n}, Age: {a}".format(n="Sara", a=22))

🔹 f-Strings (Python 3.6+)

The cleanest and fastest way to format strings in modern Python.

name = "John"
score = 95

print(f"Hello {name}, your score is {score}")

✅ Output: Hello John, your score is 95


🔹 Formatting Numbers

You can format numbers (decimals, padding, etc.) using format specifiers.

pi = 3.14159
print("Value of pi: {:.2f}".format(pi))  # 2 decimal places
num = 5
print(f"{num:03}")  # Pads with zeros: 005

🔹 Aligning Strings

You can align strings using :<, :>, and :^.

txt = "Hello"
print(f"{txt:<10}")  # Left align
print(f"{txt:>10}")  # Right align
print(f"{txt:^10}")  # Center align

🔹 Escape Braces

To print curly braces {}, use double braces {{ and }}.

print("This is a {{bracket}}")

Practice Questions

Q1. Write a Python program to print "Hello, my name is Alice" using the .format() method.

Q2. Write a Python program to use keyword arguments with .format() to insert a name and age into a sentence.

Q3. Write a Python program to display the number 3.4567 rounded as 3.46 using string formatting.

Q4. Write a Python program to print 007 using string formatting to keep leading zeros.

Q5. Write a Python program to use an f-string to show a total score stored in a variable.

Q6. Write a Python program to print a centered title using the ^ alignment symbol in string formatting.

Q7. Write a Python program to format a table with aligned text and numeric values using formatting specifiers.

Q8. Write a Python program to escape curly braces in a string and display {Hello}.

Q9. Write a Python program to combine a string and an integer using string formatting.

Q10. Write a Python program to format a price to 2 decimal places and prefix it with a $ symbol.


Python String Formatting Quiz

Q1: Which method is used to insert variables into a string?

A. replace()
B. join()
C. format()
D. trim()

Q2: What do curly braces {} do in a formatted string?

A. Raise an error
B. Store comments
C. Act as placeholders
D. Define variables

Q3: Which string formatting was introduced in Python 3.6?

A. % operator
B. format()
C. f-strings
D. str()

Q4: What does {:.2f} format specifier do?

A. Converts to integer
B. Rounds to 2 decimal places
C. Adds 2 digits
D. Converts to string

Q5: What is the output of f"{5:03}"?

A. 3
B. 005
C. 5
D. 0005

Q6: How do you align text to the right in format?

A. <
B. ^
C. >
D. *

Q7: Which is NOT a valid format method?

A. format()
B. f-strings
C. % operator
D. .align()

Q8: How do you insert a literal { in format strings?

A. {
B. {}
C. {{
D. ()

Q9: What will "{name}".format(name="Alex") return?

A. Alex
B. name
C. Error
D. "name"

Q10: Which is more efficient for string formatting in Python 3.6+?

A. format()
B. join()
C. f-strings
D. printf()

Go Back Top