Skip to content

Commit e305a57

Browse files
authored
Merge pull request #489 from realpython/python-format-mini-language
Sample code for the article on the format mini-language
2 parents 2bf9eb3 + 3aeb687 commit e305a57

File tree

12 files changed

+99
-0
lines changed

12 files changed

+99
-0
lines changed

python-format-mini-language/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python's Format Mini-Language for Tidy Strings
2+
3+
This folder provides the code examples for the Real Python tutorial [Python's Format Mini-Language for Tidy Strings](https://realpython.com/python-format-mini-language/).
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
text = "Hello!"
2+
3+
# width=30
4+
print(f"{text:30}")
5+
# align="<" and width=30
6+
print(f"{text:<30}")
7+
# align="^" and width=30
8+
print(f"{text:^30}")
9+
# align=">" and width=30
10+
print(f"{text:>30}")
11+
# fill="=", align="^" and width=30
12+
print(f"{text:=^30}")
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
inventory = [
2+
{"product": "Apple", "price": 5.70},
3+
{"product": "Orange", "price": 4.50},
4+
{"product": "Banana", "price": 6.00},
5+
{"product": "Mango", "price": 8.60},
6+
{"product": "Pepper", "price": 4.20},
7+
{"product": "Carrot", "price": 3.57},
8+
]
9+
for item in inventory:
10+
product = item["product"]
11+
price = item["price"]
12+
print(f"{product:.<30}${price:.2f}")

python-format-mini-language/date.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from datetime import datetime
2+
3+
now = datetime.now()
4+
5+
print(f"Today is {now:%a %b %d, %Y} and it's {now:%H:%M} hours")
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
total = 123456.99
2+
3+
# Formatting values
4+
width = 30
5+
align = ">"
6+
fill = "."
7+
precision = 2
8+
sep = ","
9+
10+
print(f"Total{total:{fill}{align}{width}{sep}.{precision}f}")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
number = 1234567890
2+
3+
print(f"{number:,}")
4+
print(f"{number:_}")
5+
print(f"{number:,.2f}")
6+
print(f"{number:_.2f}")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
neg_zero = -0.0
2+
3+
print(f"{neg_zero:z.4f}")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
wins = 25
2+
games = 35
3+
4+
print(f"Team's winning percentage: {wins/games:.2%}")

python-format-mini-language/person.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Person:
2+
def __init__(self, name, age):
3+
self.name = name
4+
self.age = age
5+
6+
def __str__(self):
7+
return f"I'm {self.name}, and I'm {self.age} years old."
8+
9+
def __repr__(self):
10+
return f"{type(self).__name__}(name='{self.name}', age={self.age})"
11+
12+
13+
jane = Person("Jane Doe", 25)
14+
print("Hi! {!s}".format(jane))
15+
print("An instance: {!r}".format(jane))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from math import pi
2+
3+
print(f"{pi:.4f}")
4+
print(f"{pi:.8f}")
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
number = 42
2+
print(f"int: {number:d}, hex: {number:x}, oct: {number:o}, bin: {number:b}")
3+
4+
large = 1234567890
5+
print(f"{large:e}")
6+
print(f"{large:E}")
7+
8+
number = 42.42
9+
print(f"{number:f}")
10+
11+
inf = float("inf")
12+
print(f"{inf:f}")
13+
print(f"{inf:F}")
14+
print(f"{large:g}")
15+
print(f"{large:G}")
16+
print(f"{number:g}")

python-format-mini-language/signs.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
positive = 42
2+
negative = -42
3+
4+
print(f"{positive:+}")
5+
print(f"{negative:+}")
6+
print(f"{positive:-}")
7+
print(f"{negative:-}")
8+
print(f"{positive: }")
9+
print(f"{negative: }")

0 commit comments

Comments
 (0)