Jinu Joseph
Jinu Joseph

Reputation: 702

How to remove white spaces within a word using python?

This is the input given John plays chess and l u d o. I want the output to be in this format (given below)

John plays chess and ludo.

I have tried Regular expression for removing spaces but doesn't work for me.

import re
sentence='John plays chess and l u d o'
sentence = re.sub(r"\s+", "", sentence, flags=re.UNICODE)

print(sentence)

I expected the output John plays chess and ludo. .
But the output I got is Johnplayschessandludo

Upvotes: 2

Views: 217

Answers (2)

Shubham Chandra
Shubham Chandra

Reputation: 75

Above code won't maintain the sequence of words while solving the problem. For example, try entering this sentence "John plays c h e s s and ludo"

Try using this instead if you have single word with whitespaces in the text at any position:

sentence = "John plays c h e s s and ludo"
sentence_list = sentence.split()
index = [index for index, item in enumerate(sentence_list) if len(item) == 1]
join_word = "".join([item for item in sentence_list if len(item) == 1])
if index != []:
    list(map(lambda x: sentence_list.pop(index[0]), index[:-1]))
    sentence_list[index[0]] = join_word
    sentence = " ".join(sentence_list)
else:
    sentence

Upvotes: 2

Devesh Kumar Singh
Devesh Kumar Singh

Reputation: 20500

This should work! In essence, the solution extracts the single characters out of the sentence, makes it a word and joins it back to the remaining sentence.

s = 'John plays chess and l u d o'

chars = []
idx = 0

#Get the word which is divided into single characters
while idx < len(s)-1:

    #This will get the single characters around single spaces
    if s[idx-1] == ' ' and s[idx].isalpha() and s[idx+1] == ' ':
        chars.append(s[idx])

    idx+=1

#This is get the single character if it is present as the last item
if s[len(s)-2] == ' ' and s[len(s)-1].isalpha():
    chars.append(s[len(s)-1])

#Create the word out of single character
join_word = ''.join(chars)

#Get the other words
old_words = [item for item in s.split() if len(item) > 1]

#Form the final string
res = ' '.join(old_words + [join_word])

print(res)

The output will then look like

John plays chess and ludo

Upvotes: 4

Related Questions