8000 Sample code for the article on the string data type · realpython/materials@8ff7ddd · GitHub
[go: up one dir, main page]

Skip to content

Commit 8ff7ddd

Browse files
committed
Sample code for the article on the string data type
1 parent cfdaba7 commit 8ff7ddd

File tree

7 files changed

+91
-0
lines changed

7 files changed

+91
-0
lines changed

python-string/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Strings and Character Data in Python
2+
3+
This folder provides the code examples for the Real Python tutorial [Strings and Character Data in Python](https://realpython.com/python-string/).

python-string/concatenation.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
greeting = "Hello"
2+
name = "Pythonista"
3+
print(greeting + ", " + name + "!!!")

python-string/formatting copy.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+
print(format(1000000, ",.2f")) # Thousand separators
4+
print(format("Header", "=^30")) # Centered and filled
5+
print(format(datetime.now(), "%a %b %d, %Y")) # Date

python-string/formatting.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ 10000 -0,0 +1,33 @@
1+
debit = 300.00
2+
credit = 450.00
3+
4+
template = """
5+
Account Report
6+
Credit: ${credit:.2f}
7+
Debit: -${debit:.2f}
8+
________________
9+
Balance: ${balance:.2f}"""
10+
11+
print(
12+
template.format(
13+
credit=credit,
14+
debit=debit,
15+
balance=credit - debit,
16+
)
17+
)
18+
19+
template = """
20+
Account Report
21+
Credit: $%(credit).2f
22+
Debit: -$%(debit).2f
23+
________________
24+
Balance: $%(balance).2f"""
25+
26+
print(
27+
template
28+
% {
29+
"credit": credit,
30+
"debit": debit,
31+
"balance": credit - debit,
32+
}
33+
)

python-string/person.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Person:
2+
def __init__(self, name, age):
3+
self.name = name
4+
self.age = age
5+
6+
def __repr__(self):
7+
return f"{type(self).__name__}(name='{self.name}', age={self.age})"
8+
9+
def __str__(self):
10+
return f"I'm {self.name}, and I'm {self.age} years old."
11+
12+
13+
john = Person("John Doe", 35)
14+
15+
print(repr(john))
16+
17+
print(str(john))

python-string/regexes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import re
2+
3+
pattern = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b"
4+
5+
regex = re.compile(pattern)
6+
7+
text = """
8+
Please contact us at support@example.com
9+
or sales@example.com for further information.
10+
"""
11+
12+
print(regex.findall(text))

python-string/table.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def display_table(data, headers):
2+
max_len = max(len(header) for header in headers)
3+
print("|".join(header.ljust(max_len) for header in headers))
4+
sep = "-" * max_len
5+
print("|".join(sep for _ in headers))
6+
for row in data:
7+
print("|".join(header.ljust(max_len) for header in row))
8+
9+
10+
data = [
11+
["Alice", "25", "Python Developer"],
12+
["Bob", "30", "Web Designer"],
13+
["Charlie", "35", "Team Lead"],
14+
]
15+
16+
headers = ["Name", "Age", "Job Title"]
17+
18+
display_table(data, headers)

0 commit comments

Comments
 (0)
0