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
11 changes: 11 additions & 0 deletions python-join-strings/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# How to Join Strings in Python

This folder contains code associated with the Real Python tutorial [How to Join Strings in Python](https://realpython.com/python-join-string/).

## About the Author

Martin Breuss - Email: [email protected]

## License

Distributed under the MIT license. See `LICENSE` for more information.
5 changes: 5 additions & 0 deletions python-join-strings/event_log.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"2025-01-24 10:00": ["click", "add_to_cart", "purchase"],
"2025-01-24 10:05": ["click", "page_view"],
"2025-01-24 10:10": ["page_view", "click", "add_to_cart"]
}
26 changes: 26 additions & 0 deletions python-join-strings/format_event_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import json


def load_log_file(file_path):
with open(file_path, mode="r", encoding="utf-8") as file:
return json.load(file)


def format_event_log(event_log):
lines = []
for timestamp, events in event_log.items():
# Convert the events list to a string separated by commas
event_list_str = ", ".join(events)
# Create a single line string
line = f"{timestamp} => {event_list_str}"
lines.append(line)

# Join all lines with a newline separator
return "\n".join(lines)


if __name__ == "__main__":
log_file_path = "event_log.json"
event_log = load_log_file(log_file_path)
output = format_event_log(event_log)
print(output)
3 changes: 3 additions & 0 deletions python-join-strings/join_concatenation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cities = ["Hanoi", "Adelaide", "Odessa", "Vienna"]
travel_path = "->".join(cities)
print(travel_path)
10 changes: 10 additions & 0 deletions python-join-strings/plus_loop_concatenation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cities = ["Hanoi", "Adelaide", "Odessa", "Vienna"]
separator = "->"

travel_path = ""
for i, city in enumerate(cities):
travel_path += city
if i < len(cities) - 1:
travel_path += separator

print(travel_path)
5 changes: 5 additions & 0 deletions python-join-strings/url_builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
base_url = "https://example.com/"
subpaths = ["blog", "2025", "01", "my-post"]

full_url = base_url + "/".join(subpaths)
print(full_url)