0

I have a list and the last list item has a \n which I don't want.

print(list)
[somebody,please,help\n]   #what I have


print(list)
[somebody,please,help]      #desired list
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
Alex Mollberg
  • 231
  • 3
  • 5
  • 9

3 Answers3

2

I think your problem is like this.

As an answer :

list[-1] = list[-1].rstrip('\n')
Community
  • 1
  • 1
dyng
  • 2,854
  • 21
  • 24
1

You can use the strip function to remove any white space characters from the ends of the string for example:

str = str.strip()

So before printing, strip the string. Perhaps you can save the stripped strings in a new list.

blaffie
  • 505
  • 1
  • 10
  • 32
  • Also check out the accepted answer [here](http://stackoverflow.com/questions/1185524/how-to-trim-whitespace-including-tabs) – blaffie Jan 18 '13 at 06:15
0

str.strip() will remove the white space chars from the ends of the string. Don't forget that strip() returns a new string which needs to be assigned... You probably want.

for element in list:
    element = element.strip()
mbowden
  • 687
  • 6
  • 7