Sunday 19 November 2017

Substitute multiple whitespace with single whitespace in Python





I have this
string:



mystring = 'Here is some
text I wrote
'



How can I
substituate the double, triple (...) whitespaces to just one whitespace so that I
get:



mystring = 'Here is some text
I wrote'

class="post-text" itemprop="text">
class="normal">Answer



A simple
possibility (if you'd rather avoid REs)
is



'
'.join(mystring.split())



The
split and join perform the task you're explicitly asking about -- plus, they also do the
extra one that you don't talk about but is seen in your example, removing trailing
spaces;-).


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...