FAQ: String Methods - .strip()

Let me give another example, in one of the previous questions we run this following code -

For split:

line_one = "The sky has given over" line_one_words = line_one.split() print(line_one_words) print(line_one.split()) #Both creating a new variable or printing with the string method works.

Works like I thought it would.

For join:

reapers_line_one_words = ["Black", "reapers", "with", "the", "sound", "of", "steel", "on", "stones"] reapers_line_one = " ".join(reapers_line_one_words) print(reapers_line_one) print(" ".join(reapers_line_one_words)) #Both creating a new variable or printing with the string method works.

Works as I thought it would.

But then on the next question, for strip… it doesn’t at all.

love_maybe_lines = ['Always ', ' in the middle of our bloodiest battles ', 'you lay down your arms', ' like flowering mines ','\n' ,' to conquer me home. '] love_maybe_lines_stripped = love_maybe_lines.strip() print(love_maybe_lines.strip())

When I’m trying to remove the white space - it’ll give me an error with either line 3, or line 4. Whether I’m trying to create a new variable or print out the stripped version of it.

Please post raw code, not codebytes.

Examples of join -

reapers_line_one_words = [“Black”, “reapers”, “with”, “the”, “sound”, “of”, “steel”, “on”, “stones”]
reapers_line_one = " “.join(reapers_line_one_words)
print(” ".join(reapers_line_one_words))

Examples of split -

line_one = “The sky has given over”
line_one_words = line_one.split()
print(line_one_words)
print(line_one.split())

Examples of strip -

love_maybe_lines = ['Always ‘, ’ in the middle of our bloodiest battles ‘, ‘you lay down your arms’, ’ like flowering mines ‘,’\n’ ,’ to conquer me home. ']
love_maybe_lines_stripped = love_maybe_lines.strip()
print(love_maybe_lines.strip())

Why does strip error back to -

Traceback (most recent call last):
File “main.py”, line 3, in
love_maybe_lines_stripped = love_maybe_lines.strip()
AttributeError: ‘list’ object has no attribute ‘strip’

Whereas the others have no issue creating new variables or printing with the method attached

That is a list of strings. To use .strip() you will need to iterate the list.

1 Like

A possible solution and code breakdown below:

love_maybe_lines_stripped = []
for i in range(len(love_maybe_lines)):
  stripped_str = love_maybe_lines[i].strip()
  love_maybe_lines_stripped.append(stripped_str)
love_maybe_full = '\n'.join(love_maybe_lines_stripped)
print(love_maybe_full)

Line 1: Empty list stored in a variable to append into.

Line 2: Start a for loop using range to iterate through the provided list (love_maybe_lines).

Line 3: Store each string in the list, stripping it, in a variable (One at a time per loop).

Line 4: Per loop, append each stripped string as executed in line 3 to the empty list (love_maybe_lines_stripped).

Line 5: Join all stripped strings into a single string using .join() with a delimiter of \n, forcing all strings to start in a new line. Store that single string in a var (love_maybe_full).

Line 6: Print love_maybe_full var.