Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Add Space Between Numbers?
#1
I have phone extensions that need a space between the as they are printed out. for example, instead of "my extension is 123" have it printed out as "my extension is 1 2 3".

where can i look to split numbers with a whitespace between them?
Reply
#2
text = "my extension is 1 2 3".split()
new_txt = f'{" ".join(text[:-3])} {"".join(text[-3:])}'
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
I appreciate the help however I need to add space between the numbers and the reply will likely have the extension in different locations within the string.

from "My extension is 123" to "My extension is 1 2 3"
or "123 is my extension" to "1 2 3 is my extension"

In c# I would parse the string to an array and replace each number with a split(), but i see python's split() works a bit differently. I'm just not sure where to look for it in python and with all the built in functions in there I wouldn't be surprised if something already exists.
Reply
#4
Here a couple of ways.
>>> text = "my extension is 123"
>>> s = text.partition('123')
>>> s[0] + ' '.join(list(s[1]))
'my extension is 1 2 3'
Regex.
>>> import re
>>> 
>>> text = "my extension is 123"
>>> re.sub(r'(\d)', r'\1 ', text)[:-1]
'my extension is 1 2 3'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  from global space to local space Skaperen 4 2,312 Sep-08-2020, 04:59 PM
Last Post: Skaperen
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,725 May-09-2019, 12:19 PM
Last Post: Pleiades

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020