0

I have strings which could have single, double or triple white spaces which I want to replace with a comma ','

I have used .replace(' ', ',') like so

white_space  ="this has white spaces"          #this has white spaces
white_space_replaced = white_space.replace(' ', ',') #this,has,white,spaces

double_white_space  ="this  has  double  white  spaces" #this  has  double  white  spaces
double_white_space_replaced = double_white_space.replace(' ', ',') #this,,has,,double,,white,,spaces

But I want to only replace the white spaces with a single comma. How can I cater for multiple sizes of whitespaces in python3?

khelwood
  • 55,782
  • 14
  • 81
  • 108
Harry Boy
  • 4,159
  • 17
  • 71
  • 122

1 Answers1

0

Use This, This may work for your case: :)

",".join(double_white_space.split(" "))
Jovan
  • 763
  • 7
  • 26