How does strip()

In step 8, I coded this:

for i in daily_transactions_split:
  for j in i:
    transactions_clean.append(j.strip())

and after that, all the data was clean.
However, before I wrote this code, print(daily_transactions_split) would show in some strings “\n”, which which wouldn’t be with the code above. So, why did .strip() strip the escape characters?

The Python string method .strip() removes all leading and trailing whitespace from the specified string. This includes tabs and newline characters. \n is an escape sequence used to specify a new line. \n is not a string literal that consists of a backslash followed by an “n”, but rather an escape sequence that specifies a new line.

print("Hello\nWorld!")

# prints:
# Hello
# World!

More on escape sequences here.

Welcome to the Codecademy Forums!

1 Like