How to remove one space between text

This is the input entered "S H A N N O N B R A D L E Y" (one space between each letter, but 2 spaces between SHANNON and BRADLEY) I want the output to be in this format (below)

SHANNON BRADLEY

Any way to do this in R or Python

+4
source share
9 answers

try this in R

text <- c("S H A N N O N  B R A D L E Y")

gsub(" (?! )" , text , replacement = "" , perl = T)

this is another simple

gsub("\\b " , text , replacement = "" , perl = T)
+9
source

If all letters are separated by one space and by two words in all words, you can do something like this:

In [2]: "S H A N N O N  B R A D L E Y".replace('  ', '   ')[::2]
Out[2]: 'SHANNON BRADLEY'
+5
source

python, :

    import re
    s = "S H A N N O N  B R A D L E Y"
    ' '.join([''.join(i.split()) for i in re.split(r' {2,}',s)])
+2

Python:

import sys

myText="S H A N N O N  B R A D L E Y" 
for i in range(len(myText)):
    if (myText[i]!=" " or myText[i+1]==" "):
        sys.stdout.write(myText[i])
    else:
        pass

:

enter image description here

+2

Python:

" ".join(["".join(w.split(" ")) for w in "S H A N N O N  B R A D L E Y".split("  ")])

.

: , , .

+2

Another option in Rc gsub:

gsub(" ([^ ])", "\\1", "S H A N N O N  B R A D L E Y")
# [1] "SHANNON BRADLEY"
+1
source

In R:

x <- "S H A N N O N     B R A D L E Y"
paste(gsub(' ', '', unlist(strsplit(x, split = '     '))), collapse = ' ')
0
source

Python implementation:

import re
re.sub(' (?! )', '', "S H A N N O N  B R A D L E Y")
#"SHANNON BRADLEY"
0
source

In python

In [1]: s="S H A N N O N     B R A D L E Y"
In [2]: "".join( [i.replace(' ','')  if i !='' else i.replace('',' ') for i in s.split('  ') ])
Out[2]: 'SHANNON BRADLEY'
0
source

All Articles