How can I remove the ‘\n’ that appears on the list?
love_maybe_lines = \
['Always ', ' in the middle of our bloodiest battles ', 'you lay down your arms', ' like flowering mines ','\n' ,' to conquer me home. ']
This is my code to strip every line and make multiline string, but I’m not sure on how to remove the ‘\n’ that appears on my list.
love_maybe_lines_stripped = \
[l.strip() for l in love_maybe_lines]
love_maybe_full = '\n'.join(love_maybe_lines_stripped)
print(love_maybe_full)
It, like other white space will be removed with l.strip(). We then put it back in on each break in the finished string so it still appears on multiple lines.
Why is my answer for #2 incorrect when it prints the correct output?
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 = [x.strip() for x in love_maybe_lines if x != '\n']
print(love_maybe_lines_stripped)
print()
love_maybe_full = ' '.join(love_maybe_lines_stripped)
print(love_maybe_full)
print("Always in the middle of our bloodiest battles you lay down your arms like flowering mines to conquer me home.")
Last print is what view solution provided, and the print above it is my solution. They both match. So why is it flagging incorrect when the output matches?
I feel like whoever did the test for the String section are way to strict and did them to expect and exact result when in every programming language there are numerous ways to get the same result. Why does it all or nothing to pass this if you don’t type in
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 = []
for line in love_maybe_lines:
love_maybe_lines_stripped.append(line.strip())
love_maybe_full = '\n'.join(love_maybe_lines_stripped)
print(love_maybe_full)
This to me is like telling a foreigner who can’t speak the dialect of English well that they are saying it wrong because you don’t like how they say the words, when in fact the words are the same. My code is doing the same thing. I am speaking a different dialect, but I am saying the same thing in the end result. So why are you flagging me for doing something wrong, when I am doing it right?
I am getting the right answer for the first step, but it’s fairly different from the solution. Adds more lines
of code.
I tend to approach these problems in my own way to get to the solutions. Should I focus more on the code provided to learn, or are those just there in case you’re unable to progress?
In some instances we will be required to follow instructions, in others the floor is open. If you comprehend a given solution well enough to explain everything it is doing, then by all means explore other ideas and possibilities. Just don’t let your confidence give you a false sense of security (correction bias) and be vigilante with testing.