-2
newContents = ['The', 'crazy', 'panda', 'walked', 'to', 'the', 'Maulik', 'and', 'then', 'picked.', 'A', 'nearby', 'Ankur', 'was\n', 'unaffected', 'by', 'these', 'events.\n']
print(' '.join(newContents))

output:

The crazy panda walked to the Maulik and then picked. A nearby Ankur was
 unaffected by these events.

there is space before the (first) word unaffected on second line I don't want a space there.

gondu riya
  • 49
  • 2
  • 6

5 Answers5

4

There's a simple enough solution: replace \n[space] with \n. That way all spaces are left alone and only string replaced is \n[space] with newline without space

>>> newContents = ['The', 'crazy', 'panda', 'walked', 'to', 'the', 'Maulik', 'and', 'then', 'picked.', 'A', 'nearby', 'Ankur', 'was\n', 'unaffected', 'by', 'these', 'events.\n']
>>> print(' '.join(newContents).replace('\n ', '\n'))
The crazy panda walked to the Maulik and then picked. A nearby Ankur was
unaffected by these events.
Piotr Kamoda
  • 956
  • 1
  • 9
  • 24
3

You could remove it after join:

your_string = ' '.join(newContents).replace('\n ', '\n')
print(your_string)
Oleksandr Dashkov
  • 2,249
  • 1
  • 15
  • 29
1

You could use replace to check for a space after a newline:

print(' '.join(newContents).replace('\n ', '\n'))

It outputs :

The crazy panda walked to the Maulik and then picked. A nearby Ankur was
unaffected by these events.
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
1

Use re.sub function to remove spaces right after newline:

import re

newContents = ['The', 'crazy', 'panda', 'walked', 'to', 'the', 'Maulik', 'and', 'then', 'picked.', 'A', 'nearby', 'Ankur', 'was\n', 'unaffected', 'by', 'these', 'events.\n']
print(re.sub(r'\n\s+', '\n',' '.join(newContents)))

The output:

The crazy panda walked to the Maulik and then picked. A nearby Ankur was
unaffected by these events.

The above will also remove multiple spaces(if occur) after newline

RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
-1

Strip the whitespace from each one:

>>> newContents = ['The', 'crazy', 'panda', 'walked', 'to', 'the', 'Maulik', 'and', 'then', 'picked.', 'A', 'nearby', 'Ankur', 'was\n', 'unaffected', 'by', 'these', 'events.\n']
>>> print(' '.join(item.strip() for item in newContents))
The crazy panda walked to the Maulik and then picked. A nearby Ankur was unaffected by these events.
James Scholes
  • 7,686
  • 3
  • 19
  • 20
  • you do not need to create the list. Just provide an iterator like `' '.join(x.strip() for x in newContents)` – Ma0 Feb 27 '17 at 13:04
  • 2
    @Ev.Kounis - `str.join` sometimes performs better when passed a list. That said, this answer is wrong, because it removes the linefeeds. The question only wants to get rid of the extraneous spaces. – TigerhawkT3 Feb 27 '17 at 13:06
  • As stated in comments, OP wants to keep newline. Just don't want the space before newline – Lafexlos Feb 27 '17 at 13:06
  • @TigerhawkT3 That is very interesting.. Do you by any chance have a reference for that? – Ma0 Feb 27 '17 at 13:07
  • http://stackoverflow.com/questions/34822676/joining-strings-generator-or-list-comprehension – Jean-François Fabre Feb 27 '17 at 13:10