From 1f7e8ec11db78871f08fa4a8f9d42984543a4e11 Mon Sep 17 00:00:00 2001 From: Gian Palmares <134014320+GianPalmares@users.noreply.github.com> Date: Sun, 12 Nov 2023 21:07:25 +0000 Subject: [PATCH] Create StringMethods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Preserve the Verse has one final task for you. They have delivered you a string that contains a list of poems, titled highlighted_poems, they want to highlight on the site, but they need your help to parse the string into something that can display the name, title, and publication date of the highlighted poems on the site. First, start by printing highlighted_poems to the terminal and see how it displays. Checkpoint 2 Passed 2. The information for each poem is separated by commas, and within this information is the title of the poem, the author, and the date of publication. Start by splitting highlighted_poems at the commas and saving it to highlighted_poems_list. . 3. Print highlighted_poems_list, how does the structure of the data look now? Checkpoint 4 Passed 4. Notice that there is inconsistent whitespace in highlighted_poems_list. Let’s clean that up. Start by creating a new empty list, highlighted_poems_stripped. Then, iterate through highlighted_poems_list using a for loop and for each poem strip away the whitespace and append it to your new list, highlighted_poems_stripped. 5. Print highlighted_poems_stripped. Looks good! All the whitespace is cleaned up. Checkpoint 6 Passed 6. Next we want to break up all the information for each poem into it’s own list, so we end up with a list of lists. Create a new empty list called highlighted_poems_details. Checkpoint 7 Passed 7. Iterate through highlighted_poems_stripped and split each string around the : characters and append the new lists into highlighted_poems_details. Checkpoint 8 Passed 8. Great! Now we want to separate out all of the titles, the poets, and the publication dates into their own lists. Create three new empty lists, titles, poets, and dates. Checkpoint 9 Passed 9. Iterate through highlighted_poems_details and for each list in the list append the appropriate elements into the lists titles, poets, and dates. For example, for the poem The Shadow (1915) by William Carlos Williams your code should be adding "The Shadow" to titles, "William Carlos Williams" to poets, and "1915" to dates. Checkpoint 10 Passed 10. Finally, write a for loop that uses .format() to print out the following string for each poem: The poem TITLE was published by POET in DATE. --- StringMethods | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 StringMethods diff --git a/StringMethods b/StringMethods new file mode 100644 index 0000000..a7136a4 --- /dev/null +++ b/StringMethods @@ -0,0 +1,39 @@ +highlighted_poems = "Afterimages:Audre Lorde:1997, The Shadow:William Carlos Williams:1915, Ecstasy:Gabriela Mistral:1925, Georgia Dusk:Jean Toomer:1923, Parting Before Daybreak:An Qi:2014, The Untold Want:Walt Whitman:1871, Mr. Grumpledump's Song:Shel Silverstein:2004, Angel Sound Mexico City:Carmen Boullosa:2013, In Love:Kamala Suraiyya:1965, Dream Variations:Langston Hughes:1994, Dreamwood:Adrienne Rich:1987" + +# print(highlighted_poems) + +highlighted_poems_list = highlighted_poems.split(",") + +# print(highlighted_poems_list) + +highlighted_poems_stripped = [] +highlighted_poems_details = [] +titles = [] +poets = [] +dates = [] + +for space in highlighted_poems_list: + highlighted_poems_stripped.append(space.strip()) + +# print(highlighted_poems_stripped) + +for colon in highlighted_poems_stripped: + highlighted_poems_details.append(colon.split(":")) + +# print(highlighted_poems_details) + +for details in highlighted_poems_details: + titles.append(details[0]) + poets.append(details[1]) + dates.append(details[2]) + +# print(titles) +# print(poets) +# print(dates) + +# for output in range(len(titles)): +# new_string = "The poem {titles} was published by {poets} in {dates}".format(titles = titles[output], poets = poets[output], dates = dates[output]) +# print(new_string) + +for output in range(len(titles)): + print(f"The poem {titles[output]} was published by {poets[output]} in {dates[output]}")