From 6afb7d2022543632db6ee8a89179fdd1958e0df6 Mon Sep 17 00:00:00 2001 From: martin-martin Date: Wed, 22 Jan 2025 12:31:14 +0100 Subject: [PATCH 1/4] Add code for string join tutorial --- python-join-strings/README.md | 11 +++++++++++ python-join-strings/event_log.json | 5 +++++ python-join-strings/format_event_log.py | 26 +++++++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 python-join-strings/README.md create mode 100644 python-join-strings/event_log.json create mode 100644 python-join-strings/format_event_log.py diff --git a/python-join-strings/README.md b/python-join-strings/README.md new file mode 100644 index 0000000000..8c8c0a8593 --- /dev/null +++ b/python-join-strings/README.md @@ -0,0 +1,11 @@ +# How to Join a String in Python + +This folder contains code associated with the Real Python tutorial on [How to Join a String in Python](https://realpython.com/python-join-string/). + +## About the Author + +Martin Breuss - Email: martin@realpython.com + +## License + +Distributed under the MIT license. See `LICENSE` for more information. \ No newline at end of file diff --git a/python-join-strings/event_log.json b/python-join-strings/event_log.json new file mode 100644 index 0000000000..2b46d4d9fe --- /dev/null +++ b/python-join-strings/event_log.json @@ -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"] +} \ No newline at end of file diff --git a/python-join-strings/format_event_log.py b/python-join-strings/format_event_log.py new file mode 100644 index 0000000000..9436ca17a7 --- /dev/null +++ b/python-join-strings/format_event_log.py @@ -0,0 +1,26 @@ +import json + + +def load_log_file(file_path): + with open(file_path, mode="r", encoding="utf-8") as event_log_file: + return json.load(event_log_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) From 7350552601153ef7cba30d216a7d9d5ab701743f Mon Sep 17 00:00:00 2001 From: martin-martin Date: Fri, 24 Jan 2025 12:39:13 +0100 Subject: [PATCH 2/4] Add code files --- python-join-strings/join_concatenation.py | 3 +++ python-join-strings/plus_loop_concatenation.py | 10 ++++++++++ python-join-strings/url_builder.py | 5 +++++ 3 files changed, 18 insertions(+) create mode 100644 python-join-strings/join_concatenation.py create mode 100644 python-join-strings/plus_loop_concatenation.py create mode 100644 python-join-strings/url_builder.py diff --git a/python-join-strings/join_concatenation.py b/python-join-strings/join_concatenation.py new file mode 100644 index 0000000000..8d50f7a856 --- /dev/null +++ b/python-join-strings/join_concatenation.py @@ -0,0 +1,3 @@ +cities = ["Hanoi", "Adelaide", "Odessa", "Vienna"] +travel_path = "->".join(cities) +print(travel_path) diff --git a/python-join-strings/plus_loop_concatenation.py b/python-join-strings/plus_loop_concatenation.py new file mode 100644 index 0000000000..88e4865bf4 --- /dev/null +++ b/python-join-strings/plus_loop_concatenation.py @@ -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) diff --git a/python-join-strings/url_builder.py b/python-join-strings/url_builder.py new file mode 100644 index 0000000000..2787429596 --- /dev/null +++ b/python-join-strings/url_builder.py @@ -0,0 +1,5 @@ +base_url = "https://example.com/" +subpaths = ["blog", "2025", "01", "my-post"] + +full_url = base_url + "/".join(subpaths) +print(full_url) From 7a1d50269d6ad4c66f212a5d8418af69e4535765 Mon Sep 17 00:00:00 2001 From: martin-martin Date: Fri, 24 Jan 2025 12:42:36 +0100 Subject: [PATCH 3/4] Update code file after TR --- python-join-strings/format_event_log.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/python-join-strings/format_event_log.py b/python-join-strings/format_event_log.py index 9436ca17a7..9c13ecc215 100644 --- a/python-join-strings/format_event_log.py +++ b/python-join-strings/format_event_log.py @@ -2,20 +2,20 @@ def load_log_file(file_path): - with open(file_path, mode="r", encoding="utf-8") as event_log_file: - return json.load(event_log_file) + 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. + # Convert the events list to a string separated by commas event_list_str = ", ".join(events) - # Create a single line string. + # Create a single line string line = f"{timestamp} => {event_list_str}" lines.append(line) - # Join all lines with a newline separator. + # Join all lines with a newline separator return "\n".join(lines) From 33aec44ff945fc599c676be846010a7db720c5b3 Mon Sep 17 00:00:00 2001 From: brendaweles <160772586+brendaweles@users.noreply.github.com> Date: Mon, 3 Feb 2025 12:11:05 -0700 Subject: [PATCH 4/4] Update README.md --- python-join-strings/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python-join-strings/README.md b/python-join-strings/README.md index 8c8c0a8593..bf18907157 100644 --- a/python-join-strings/README.md +++ b/python-join-strings/README.md @@ -1,6 +1,6 @@ -# How to Join a String in Python +# How to Join Strings in Python -This folder contains code associated with the Real Python tutorial on [How to Join a String in Python](https://realpython.com/python-join-string/). +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 @@ -8,4 +8,4 @@ Martin Breuss - Email: martin@realpython.com ## License -Distributed under the MIT license. See `LICENSE` for more information. \ No newline at end of file +Distributed under the MIT license. See `LICENSE` for more information.