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.

what do you mean by that?

clean_line = line.strip()

or,

lst.append(line.strip()
1 Like

Why is new line character considered as a white space and removed with .strip() ?

n = “\n1\n2\n3\n4\n \n”
print(“->” + n.strip() + “<-”)

should be equal to

print(“->” + n + “<-”)

instead

n = "\n1\n2\n3\n4\n \n" print("->" + n.strip() + "<-") print("->" + n + "<-")

From documentation of str.strip([chars]):

Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None , the chars argument defaults to removing whitespace.

From documentation of string.whitespace:

A string containing all ASCII characters that are considered whitespace. This includes the characters space, tab, linefeed, return, formfeed, and vertical tab.

import string
s = string.whitespace
print(repr(s))

// Output: 
' \t\n\r\x0b\x0c'

// Your string:
n = "\n1\n2\n3\n4\n \n"
// Leading whitespace in your string:
"\n"
// Trailing whitespace in your string:
"\n \n"
// After stripping, the remaining string is:
"1\n2\n3\n4"
1 Like

Thanks a lot. Awesome explanation.

1 Like