I have a string:
"foo (2 spaces) bar (3 spaces) baaar (6 spaces) fooo"
How do I remove repetitious spaces in it so there should be no more than one space between any two words?
You can also use the string split() and join() functions to remove multiple spaces from a string.
The metacharacter “\s” matches spaces and + indicates the occurrence of the spaces one or more times, therefore, the regular expression \S+ matches all the space characters (single or multiple). Therefore, to replace multiple spaces with a single space.
Using re. sub() or split() + join() method to remove extra spaces between words in Python.
String#squeeze has an optional parameter to specify characters to squeeze.
irb> "asd asd asd asd".squeeze(" ") => "asd asd asd asd"
Warning: calling it without a parameter will 'squezze' ALL repeated characters, not only spaces:
irb> 'aaa bbbb cccc 0000123'.squeeze => "a b c 0123"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With