Logo

Programming-Idioms

History of Idiom 219 > diff from v38 to v39

Edit summary for version 39 by programming-idioms.org:
[Python] Python 2 is deprecated, see issues/184

Version 38

2021-12-23, 09:05:18

Version 39

2021-12-23, 09:06:17

Idiom #219 Replace multiple spaces with single space

Create string t from the value of string s with each sequence of spaces replaced by a single space.

Explain if only the space characters will be replaced, or the other whitespaces as well: tabs, newlines.

Idiom #219 Replace multiple spaces with single space

Create string t from the value of string s with each sequence of spaces replaced by a single space.

Explain if only the space characters will be replaced, or the other whitespaces as well: tabs, newlines.

Extra Keywords
collapse repeated
Extra Keywords
collapse repeated
Imports
import re
Imports
import re
Code
t = re.sub(' +', ' ', s)
Code
t = re.sub(' +', ' ', s)
Comments bubble
Only replaces spaces.

Works in Python 2 & Python 3.
Comments bubble
Only replaces spaces.
Demo URL
https://replit.com/@ProgIdioms/RustyWingedNanocad#main.py
Demo URL
https://replit.com/@ProgIdioms/RustyWingedNanocad#main.py