-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #554 from realpython/python-string
Sample code for the article on the string data type
- Loading branch information
Showing
7 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
greeting = "Hello" | ||
name = "Pythonista" | ||
print(greeting + ", " + name + "!!!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |