Does .strip() default argument remove \n characters?

I am working on Task 8 of the Thread Shed project in the Strings lesson in the Learn Python 3 course.

Part of the goal of the task is to get rid of the extra whitespace in the data points in the list (within the larger list). The first block of code successfully does that, as does the second block of code (verified by the “Test” block of code). In the video walkthrough of the project, the author uses the second block of code to get rid of the newline characters, however my block 1 code below did that without needing the “.replace()” function. I am wondering why… why don’t I need the replace function to get the right data? Is this an update that new versions of Python implemented? Does the default argument of .strip() get rid of whitespace as well as newline characters?

Thanks!

Block 1

transactions_clean =
for transaction in daily_transactions_split:
transaction_clean =
for data_point in transaction:
transaction_clean.append(data_point.strip())
transactions_clean.append(transaction_clean)
print(transactions_clean)

Block 2

transactions_clean2 =
for transaction in daily_transactions_split:
transaction_clean2 =
for data_point in transaction:
transaction_clean2.append(data_point.replace(’\n’, ’ ').strip())
transactions_clean2.append(transaction_clean2)
print(transactions_clean2)

Test

if transactions_clean == transactions_clean2:
print(True)
else:
print(False)

Since newline is considered white space, yes.

>>> x = '   string   \n'
>>> x.strip()
'string'
>>> 

If you can track down Python 2 lesson, we can try it there, too, just to see if this is one of the changes in Python 3.

1 Like