Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python remove separating spaces [closed]

Tags:

python

You may or may not have heard of Willie, the Python IRC bot.

I have a trigger.group(2) object:

'J O I N   : # S t a f f'

How do I make this become:

'JOIN :#Staff'

Edit: Basically I want to remove the spaces between the characters without removing the "real" spaces, between N :. Note that the command could be anything with any number of arguments: PRIVMSG, OPER, etc.

like image 360
abestic9 Avatar asked Sep 23 '26 19:09

abestic9


2 Answers

Since you have alternating spaces and non-spaces, try taking every second character:

s = s[::2]

Example:

>>> 'J O I N   : # S t a f f'[::2]
'JOIN :#Staff'
like image 147
nneonneo Avatar answered Sep 25 '26 10:09

nneonneo


>>> import re
>>> text = 'J O I N   : # S t a f f'
>>> re.sub(r'(?<=\S)\s|\s(?=\S)', '', text)
'JOIN :#Staff'
like image 34
jamylak Avatar answered Sep 25 '26 09:09

jamylak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!