Skip to content

Commit

Permalink
Merge pull request #554 from realpython/python-string
Browse files Browse the repository at this point in the history
Sample code for the article on the string data type
  • Loading branch information
brendaweles authored Jul 22, 2024
2 parents bd32d97 + ed8d925 commit 64b878d
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 0 deletions.
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 support@example.com
or sales@example.com 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)

0 comments on commit 64b878d

Please sign in to comment.