Skip to content

Commit 89b9bfc

Browse files
authored
Merge branch 'master' into python-string-formatting-update
2 parents cff2fe2 + 8fbe445 commit 89b9bfc

File tree

9 files changed

+138
-0
lines changed

9 files changed

+138
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# String Interpolation in Python: Exploring Available Tools
2+
3+
This folder provides the code examples for the Real Python tutorial [String Interpolation in Python: Exploring Available Tools](https://realpython.com/python-string-interpolation/).
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Article:
2+
def __init__(self, title, author, pub_date):
3+
self.title = title
4+
self.author = author
5+
self.pub_date = pub_date
6+
7+
def __str__(self):
8+
return (
9+
f"Article: {self.title}\n"
10+
f"Author: {self.author}\n"
11+
f"Published: {self.pub_date}\n"
12+
)
13+
14+
def __repr__(self):
15+
return (
16+
f"{type(self).__name__}("
17+
f"title={self.title}, "
18+
f"author={self.author}, "
19+
f"pub_date={self.pub_date})"
20+
)
21+
22+
23+
if __name__ == "__main__":
24+
article = Article(
25+
title="String Interpolation in Python: Exploring Available Tools",
26+
author="Real Python",
27+
pub_date="2024-05-15",
28+
)
29+
30+
print(f"{article!s}")
31+
print(f"{article!r}")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name = "Pythonista"
2+
day = "Friday" # Of course 😃
3+
4+
print("Hello, " + name + "! Today is " + day + ".")
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import csv
2+
3+
template = """
4+
Dear {customer},
5+
6+
Thank you for your recent purchase of {product}.
7+
8+
Remember, our support team is always here to assist you.
9+
10+
Best regards,
11+
{employee}
12+
"""
13+
14+
15+
def display_emails(template, data_file):
16+
with open(data_file) as file:
17+
for customer in csv.DictReader(file):
18+
print(template.format(**customer))
19+
20+
21+
display_emails(template, "sales.csv")
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import math
2+
3+
name = "Pythonista"
4+
day = "Friday" # Of course 😃
5+
print(f"Hello, {name}! Today is {day}.")
6+
7+
x = 5
8+
y = 10
9+
print(f"The sum of {x} and {y} is {x + y}.")
10+
11+
12+
radius = 16
13+
print(f"The area of your circle is {math.pi * radius ** 2}")
14+
15+
name = "Pythonista"
16+
site = "real python"
17+
print(f"Hello, {name.upper()}! Welcome to {site.title()}!")
18+
print(f"{[2**n for n in range(3, 9)]}")
19+
20+
print(f"The number is {42}")
21+
print(f"Pi {3.14}")
22+
23+
value = "Suspicious value"
24+
print(f"{value = }")
25+
print(f"{2 + 3 = }")
26+
27+
numbers = {"one": 1, "two": 2, "three": 3}
28+
print(f"{numbers['one']}-{numbers['two']}-{numbers['three']}")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name = "Pythonista"
2+
day = "Friday" # Of course 😃
3+
4+
print("Hello, {}! Today is {}.".format(name, day))
5+
6+
7+
numbers = {"one": 1, "two": 2, "three": 3}
8+
print("{one}-{two}-{three}".format(**numbers))
9+
print("{one}-{two}".format(**numbers))
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name = "Pythonista"
2+
day = "Friday" # Of course 😃
3+
4+
print("Hello, %s! Today is %s." % (name, day))
5+
6+
x = 5
7+
y = 10
8+
print("The sum of %s and %s is %s." % (x, y, x + y))
9+
10+
template = "The sum of %s and %s is %s."
11+
print(template % (x, y, x + y))
12+
13+
print("Hello, %s!" % "Pythonista")
14+
print("Hello, %s!" % ("Pythonista",))
15+
16+
# print("Interpolating a tuple: %s" % (1, 2, 3))
17+
print("Interpolating a tuple: %s" % ((1, 2, 3),))
18+
19+
jane = {"name": "Jane", "job": "Python Dev"}
20+
print("My name is %(name)s. I'm a %(job)s." % jane)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
product,customer,employee
2+
MacBook Pro 16-inch,Emily,John
3+
Samsung Galaxy S22,Linda,Jane
4+
Canon EOS R5,Bob,Kate
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from string import Template
2+
3+
template = Template("Hello, $name! Today is $day.")
4+
print(template.substitute(name="John", day="Friday"))
5+
6+
print(Template("$$$amount").substitute(amount="1,000.00"))
7+
print(Template("$greeting, $who!").substitute(greeting="Hello", who="World"))
8+
9+
print(Template("${amount}USD").substitute(amount="100"))
10+
print(Template("$amountUSD").substitute(amount="100"))
11+
12+
greeting = Template("Hello, $name! Today is $day.")
13+
print(greeting.substitute(name="John", day="Friday"))
14+
greeting.template = "Hello, $name! Welcome!"
15+
print(greeting.substitute(name="John"))
16+
17+
numbers = {"one": 1, "two": 2, "three": 3}
18+
print(Template("$one-$two-$three").substitute(**numbers))

0 commit comments

Comments
 (0)