How to add strings to a list?

Compare to,

love_maybe_full = '\n'.join(love_maybe_lines_stripped)

@ruby1218558829, when you post code, it will be much more readable as Python if you use the </> icon that appears in the menu bar of the box you are typing in. Also, your code can then be copied and pasted for easy testing.

You can see what is printed including all newlines by using the repr() function with your print() statement, for example:

love_maybe_lines = ['Always    ', '     in the middle of our bloodiest battles  ', 'you lay down your arms', '           like flowering mines    ','\n' ,'   to conquer me home.    ']
def stripper(lst):
    y= []
    for x in lst:
        y.append(x.strip())
    return y
love_maybe_lines_stripped=(stripper(love_maybe_lines))
def joiner(lst):
    print(lst)
    y=''
    for x in lst:
        y=y+ ''.join([x,'\n'])
    return y
love_maybe_full=joiner(love_maybe_lines_stripped)

print(repr(love_maybe_full))

Output: (Be sure to scroll all the way to the end to see what is happening.)

'Always\nin the middle of our bloodiest battles\nyou lay down your arms\nlike flowering mines\n\nto conquer me home.\n'

Bottom line: It (the grader) does not like the fact that there is a terminal newline. If you join with ‘\n’ as suggested by @mtf, that will be avoided.

2 Likes

I am not sure why this is returning wrong. When I use the print all of the white space is gone.

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 i in love_maybe_lines:
  var = [i.strip()]
  love_maybe_lines_stripped.append(var)
  
print(love_maybe_lines_stripped)

I found my mistake. var = i.strip() instead of var = [i.strip()]

Is it possible to use two attributes with .strip() and if so how would one go about doing this?

if you mean that you want to call the strip attributes of two objects then you’d do it on one and then the other, no?

Not exactly sure if you mean, arguments? The method takes only one optional argument, the characters we wish to remove.

By playing around I just found I can use: list.strip(’:’).strip()
This allows me to run strip with an argument and a second time on the same list with a different argument.

Chaining does work, but so too does mixing the arguments…

>>> ' :!. a quick brown fox: : !: : : .:::   '.strip(': !.')
'a quick brown fox'

The order of the arguments is trivial, as well the mixture of characters to be stripped.

2 Likes

Hi, I am having some problem with Join.

First, the variable I am going to place the final string (stripped and unified) has to be initialized before?

Do I really need a for in order to execute a .join()?

What is the join syntax? I have seen people talking about brackets and I tried to change the order, but I am getting nohere.

No, you just need a list to give it as an argument, and a separator string for between the entries.

" ".join(a_list)

3 posts were split to a new topic: Difference between defining a function and simply writing a for loop

I wrote the exact same code and had the same question.

Would it be easier to use list comprehensions?

love_maybe_lines_stripped = [line.strip() for line in love_maybe_lines]

1 Like

Only if the concept has already been introduced. We do not want to race ahead else run the risk of missing important details in the naive approaches. Work with what you’ve been taught to this point.

good day. Please is this 4 arguments? i.e. are the arguments (’: ! . '). What I’m asking is if one of the arguments is a white space? Thank you. I’m sorry i know I ask way too many questions. I’m just totally new to coding

1 Like

Technically it is one argument, a single string. However, you are correct in assessing it as four separate values, space included, all of which are independently applied by the .strip() method.

1 Like

Thank you very much for clearing that up.

1 Like

That’s what I used. I am taking the course here in early 2021, and they way they have it formatted now is that you learn list comprehensions before string methods.

If that is actually the case, then this needs to be corrected.