9 Python String Formatting

1. What is String Formatting?

String formatting means putting values inside a string.

In simple words:
👉 We create a message
👉 Then we insert numbers or text into it

Example:


 

2. Old Way: Using + (Not Recommended)

Example


 

👉 This works, but it is messy.

3. Using format() Method

This is a clean and easy way.

Example


 

4. Using Index in format()

Example


 

5. Using Named Placeholders

Example


 

6. Using f-Strings (Best & Modern Way)

Available in Python 3.6+

Example


 

7. f-String with Expression

Example


 

8. f-String with Function

Example


 

9. Formatting Numbers

Example: Decimal Points


 

Output:


 

10. Formatting Date and Time

Example


 

11. Formatting with Alignment

Example


 

12. Common Beginner Mistakes

❌ Forgetting f


 

✔ Correct:


 

❌ Wrong Placeholder Count


 

❌ Extra value ignored.

13. Simple Practice Examples

Example 1: Price Message


 

Example 2: Result


 

Example 3: Table


 

14. Summary (Python String Formatting)

✔ Used to insert values in string
format() is simple
✔ f-strings are best
✔ Clean and readable code
✔ Very useful in real programs

📘 Perfect for Beginner eBook

This chapter is ideal for:

  • Python beginner books

  • School & college students

  • Self-learners

If you want next, I can write:

  • String Methods

  • File Handling

  • Exception Handling

  • Mini Python Projects

Just tell me 😊

You may also like...