diff --git a/python-string/README.md b/python-string/README.md new file mode 100644 index 0000000000..e102c575cb --- /dev/null +++ b/python-string/README.md @@ -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/). diff --git a/python-string/concatenation.py b/python-string/concatenation.py new file mode 100644 index 0000000000..21c33ddbae --- /dev/null +++ b/python-string/concatenation.py @@ -0,0 +1,3 @@ +greeting = "Hello" +name = "Pythonista" +print(greeting + ", " + name + "!!!") diff --git a/python-string/formatting copy.py b/python-string/formatting copy.py new file mode 100644 index 0000000000..141acb9138 --- /dev/null +++ b/python-string/formatting copy.py @@ -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 diff --git a/python-string/formatting.py b/python-string/formatting.py new file mode 100644 index 0000000000..c9e12d81ca --- /dev/null +++ b/python-string/formatting.py @@ -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, + } +) diff --git a/python-string/person.py b/python-string/person.py new file mode 100644 index 0000000000..e5a56797ec --- /dev/null +++ b/python-string/person.py @@ -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)) diff --git a/python-string/regexes.py b/python-string/regexes.py new file mode 100644 index 0000000000..0e26e4ddbd --- /dev/null +++ b/python-string/regexes.py @@ -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)) diff --git a/python-string/table.py b/python-string/table.py new file mode 100644 index 0000000000..acb56d2cc8 --- /dev/null +++ b/python-string/table.py @@ -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)