oldoc63 / learningDS

Learning DS with Codecademy and Books
0 stars 0 forks source link

.strip() #343

Open oldoc63 opened 1 year ago

oldoc63 commented 1 year ago

When working from strings that come from real data, you will often find that the strings aren't super clean. You'll find lots of extra whitespace, unnecessary linebreaks, and rogue tabs.

Python provides a great method for cleaning strings: .strip(). Stripping a string removes all whitespace characters from the beginning and end.

oldoc63 commented 1 year ago

All the whitespace on either side of the string has been stripped, but the whitespace in the middle has been preserved. You can also use .strip() with a character argument, which will strip that character from either end of the string.

oldoc63 commented 1 year ago

By including the argument '!' we are able to strip all the ! characters from either side of the string. Notice that now that we've included an argument we are no longer stripping whitespace, we are only stripping the argument.

oldoc63 commented 1 year ago

They sent over another list containing all the lines to the Audre Lorde poem, Love, Maybe. They want you to join together all of the lines into a single string that can be used to display the poem again, but this time, you've notice that the list contains a ton of unnecessary whitespace that doesn't appear in the actual poem.

First, use .strip() on each line in the list to remove the unnecessary whitespace and save it as a new list love_maybe_lines_stripped.

.join() the lines in love_maybe_lines_stripped together into one large multi-line string, love_maybe_full, that can be printed to display the poem.

Each line of the poem should show up on its own line.

Print love_maybe_full.