Skip to content

Sample code for the article on the string data type #554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions python-string/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Strings and Character Data in Python

This folder provides the code examples for the Real Python tutorial [Strings and Character Data in Python](https://realpython.com/python-string/).
3 changes: 3 additions & 0 deletions python-string/concatenation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
greeting = "Hello"
name = "Pythonista"
print(greeting + ", " + name + "!!!")
5 changes: 5 additions & 0 deletions python-string/formatting copy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from datetime import datetime

print(format(1000000, ",.2f")) # Thousand separators
print(format("Header", "=^30")) # Centered and filled
print(format(datetime.now(), "%a %b %d, %Y")) # Date
33 changes: 33 additions & 0 deletions python-string/formatting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
debit = 300.00
credit = 450.00

template = """
Account Report
Credit: ${credit:.2f}
Debit: -${debit:.2f}
________________
Balance: ${balance:.2f}"""

print(
template.format(
credit=credit,
debit=debit,
balance=credit - debit,
)
)

template = """
Account Report
Credit: $%(credit).2f
Debit: -$%(debit).2f
________________
Balance: $%(balance).2f"""

print(
template
% {
"credit": credit,
"debit": debit,
"balance": credit - debit,
}
)
17 changes: 17 additions & 0 deletions python-string/person.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def __repr__(self):
return f"{type(self).__name__}(name='{self.name}', age={self.age})"

def __str__(self):
return f"I'm {self.name}, and I'm {self.age} years old."


john = Person("John Doe", 35)

print(repr(john))

print(str(john))
12 changes: 12 additions & 0 deletions python-string/regexes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import re

pattern = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b"

regex = re.compile(pattern)

text = """
Please contact us at [email protected]
or [email protected] for further information.
"""

print(regex.findall(text))
18 changes: 18 additions & 0 deletions python-string/table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def display_table(data, headers):
max_len = max(len(header) for header in headers)
print("|".join(header.ljust(max_len) for header in headers))
sep = "-" * max_len
print("|".join(sep for _ in headers))
for row in data:
print("|".join(header.ljust(max_len) for header in row))


data = [
["Alice", "25", "Python Developer"],
["Bob", "30", "Web Designer"],
["Charlie", "35", "Team Lead"],
]

headers = ["Name", "Age", "Job Title"]

display_table(data, headers)
Loading