Skip to content
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 split-strings/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# How to Split a String in Python

This folder provides the code examples for the Real Python tutorial [How to Split a String in Python](https://realpython.com/python-string-split-concatenate-join/).
9 changes: 9 additions & 0 deletions split-strings/extract_errors_from_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
log_data = """2025-01-15 08:45:23 INFO User logged in
2025-01-15 09:15:42 ERROR Failed to connect to server
2025-01-15 10:01:05 WARNING Disk space running low"""

log_lines = log_data.splitlines()

for line in log_lines:
if "ERROR" in line:
print(line)
8 changes: 8 additions & 0 deletions split-strings/extract_log_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
log_line = "2025-01-15 08:45:23 INFO User logged in from IP 10.0.1.1"

date, time, log_level, message = log_line.split(maxsplit=3)

print(f"Date: {date}")
print(f"Time: {time}")
print(f"Log Level: {log_level}")
print(f"Message: {message}")
6 changes: 6 additions & 0 deletions split-strings/split_shopping_list_regex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import re

shopping_mess = "Apple :::::3:Orange | 2|||Lemon --1-Date :: 10"
shopping_list = re.split(r"\s*[:|-]+\s*", shopping_mess)

print(shopping_list)