Skip to content

Commit c628fcb

Browse files
authored
Merge pull request #627 from realpython/string-split
Add code examples for string split tutorial
2 parents 965f7bf + 695eb20 commit c628fcb

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

split-strings/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# How to Split a String in Python
2+
3+
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/).
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
log_data = """2025-01-15 08:45:23 INFO User logged in
2+
2025-01-15 09:15:42 ERROR Failed to connect to server
3+
2025-01-15 10:01:05 WARNING Disk space running low"""
4+
5+
log_lines = log_data.splitlines()
6+
7+
for line in log_lines:
8+
if "ERROR" in line:
9+
print(line)

split-strings/extract_log_info.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
log_line = "2025-01-15 08:45:23 INFO User logged in from IP 10.0.1.1"
2+
3+
date, time, log_level, message = log_line.split(maxsplit=3)
4+
5+
print(f"Date: {date}")
6+
print(f"Time: {time}")
7+
print(f"Log Level: {log_level}")
8+
print(f"Message: {message}")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import re
2+
3+
shopping_mess = "Apple :::::3:Orange | 2|||Lemon --1-Date :: 10"
4+
shopping_list = re.split(r"\s*[:|-]+\s*", shopping_mess)
5+
6+
print(shopping_list)

0 commit comments

Comments
 (0)