🐍1.4Complete Summary of String Formatting and pading in Python
🎯 Complete Summary of String Formatting in Python
Python provides multiple ways to format strings, including f-strings, .format(), and % formatting.
1️⃣ String Formatting Methods
| Method | Syntax | Description | Example Output |
|---|---|---|---|
| f-string (Basic) | f"Hello, {name}!" |
Inserts variable directly into the string | 'Hello, Alice!' |
| f-string (Expression) | f"2 + 2 = {2 + 2}" |
Evaluates expressions inside {} |
'2 + 2 = 4' |
| f-string (Formatting) | f"{num:.2f}" |
Formats numbers with 2 decimal places | '3.14' |
| .format() (Basic) | "Hello, {}!".format(name) |
Inserts variable using {} placeholders |
'Hello, Alice!' |
| .format() (Positional) | "My name is {1} and I am {0} years old".format(25, "Alice") |
Uses positional arguments | 'My name is Alice and I am 25 years old' |
| .format() (Named) | "Name: {n}, Age: {a}".format(n="Alice", a=25) |
Uses named placeholders | 'Name: Alice, Age: 25' |
| % Formatting (Old) | "Hello, %s" % name |
Inserts variable using % |
'Hello, Alice' |
| % Formatting (Numbers) | "Pi is %.2f" % 3.14159 |
Formats float with 2 decimal places | 'Pi is 3.14' |
2️⃣ String Alignment & Padding in Formatting
| Method | Syntax | Description | Example Output |
|---|---|---|---|
| Right Align (Spaces) | f"{text:>10}" |
Right-aligns text in a 10-character space | ' Alice' |
| Left Align (Spaces) | f"{text:<10}" |
Left-aligns text in a 10-character space | 'Alice ' |
| Center Align (Spaces) | f"{text:^10}" |
Centers text in a 10-character space | ' Alice ' |
| Right Align (Custom Padding) | f"{text:_>10}" |
Right-aligns with _ padding |
'_____Alice' |
| Left Align (Custom Padding) | f"{text:-<10}" |
Left-aligns with - padding |
'Alice-----' |
| Center Align (Custom Padding) | f"{text:.^10}" |
Centers text with . padding |
'..Alice...' |
Right Align (format()) |
"{:>10}".format(text) |
Right-aligns using .format() |
' Alice' |
Left Align (format()) |
"{:<10}".format(text) |
Left-aligns using .format() |
'Alice ' |
Center Align (format()) |
"{:^10}".format(text) |
Centers using .format() |
' Alice ' |
3️⃣ Number Formatting & Zero Padding
| Method | Syntax | Description | Example Output |
|---|---|---|---|
Zero Padding (zfill()) |
num.zfill(5) |
Pads numbers with leading zeros (total width = 5) | '00042' |
| Zero Padding (Negative Numbers) | "-42".zfill(6) |
Pads negative numbers with leading zeros (total width = 6) | '-00042' |
Zero Padding (f-string) |
f"{num:05d}" |
Pads numbers with zeros using f-string (total width = 5) |
'00042' |
Zero Padding (format()) |
"{:05d}".format(num) |
Pads numbers with zeros using .format() (total width = 5) |
'00042' |
📌 Key Takeaways
-
f-strings (
f"")- Best and most modern way to format strings.
- Supports expressions, formatting, and custom alignment.
-
.format()Method- More flexible than
%formatting but less concise than f-strings. - Supports positional and named placeholders.
- More flexible than
-
Old
%Formatting- Older method, still used in some cases.
%sfor strings,%dfor integers,%ffor floats.
-
String Alignment (
<,>,^)<→ Left align>→ Right align^→ Center align
-
Zero Padding (
zfill(),f"{num:0Xd}",.format())- Used for formatting numbers with leading zeros.
This table covers all formatting techniques, including alignment, padding, and zero-filling! 🚀 Let me know if you need any changes. 😊
Comments
Post a Comment