CS101: Introduction to Programming - Python String Methods (.strip())

Hi Everyone,

I am struggling to correctly strip all unnecessary whitespace from strings within a sublist. The syntax I have used is below, any review/comment will be greatly appreciated!

transactions_clean =

#first for loop is to iterate through complete daily transactions list
for transaction in daily_transactions_split:
#second for loop used to iterate through sub-lists
#will result in error if you try perform .strip on a list
#.strip needs to be performed on a string
for data in transaction:
for string in data:

When I print(transactions_clean) the white space is still present.

Thanks in advance!


In Python, strings are immutable. Therefore .strip() does not change the original string, it returns a new string. But in your code you don’t use the return value. See Built-in Types — Python 3.10.4 documentation.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.