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.
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
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).
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.
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"
love_maybe_lines = ['Always ‘, ’ in the middle of our bloodiest battles ‘, ‘you lay down your arms’, ’ like flowering mines ‘,’\n’ ,’ to conquer me home. ']