Vatsal Mishra
Vatsal Mishra

Reputation: 109

Having trouble adding a space after a period in a python string

I have to write a code to do 2 things:

  1. Compress more than one occurrence of the space character into one.

  2. Add a space after a period, if there isn't one.

For example:

input> This   is weird.Indeed
output>This is weird. Indeed.

This is the code I wrote:

def correction(string):
    list=[]
    for i in string:
        if i!=" ":
            list.append(i)
        elif i==" ":
            k=i+1
            if k==" ":
               k=""
            list.append(i)

    s=' '.join(list)
    return s

strn=input("Enter the string: ").split()
print (correction(strn))

This code takes any input by the user and removes all the extra spaces,but it's not adding the space after the period(I know why not,because of the split function it's taking the period and the next word with it as one word, I just can't figure how to fix it)

This is a code I found online:

import re
def correction2(string):
  corstr = re.sub('\ +',' ',string)
  final = re.sub('\.','. ',corstr)
  return final

strn= ("This   is as   .Indeed")
print (correction2(strn))

The problem with this code is I can't take any input from the user. It is predefined in the program. So can anyone suggest how to improve any of the two codes to do both the functions on ANY input by the user?

Upvotes: 0

Views: 4835

Answers (3)

Finwood
Finwood

Reputation: 3991

Is this what you desire?

import re

def corr(s):
    return re.sub(r'\.(?! )', '. ', re.sub(r' +', ' ', s))

s = input("> ")
print(corr(s))

I've changed the regex to a lookahead pattern, take a look here.


Edit: explain Regex as requested in comment

re.sub() takes (at least) three arguments: The Regex search pattern, the replacement the matched pattern should be replaced with, and the string in which the replacement should be done.

What I'm doing here is two steps at once, I've been using the output of one function as input of another. First, the inner re.sub(r' +', ' ', s) searches for multiple spaces (r' +') in s to replace them with single spaces. Then the outer re.sub(r'\.(?! )', '. ', ...) looks for periods without following space character to replace them with '. '. I'm using a negative lookahead pattern to match only sections, that don't match the specified lookahead pattern (a normal space character in this case). You may want to play around with this pattern, this may help understanding it better.

The r string prefix changes the string to a raw string where backslash-escaping is disabled. Unnecessary in this case, but it's a habit of mine to use raw strings with regular expressions.

Upvotes: 6

thinkerou
thinkerou

Reputation: 1877

You try the following code:

>>> s = 'This is weird.Indeed'
>>> def correction(s):
    res = re.sub('\s+$', '', re.sub('\s+', ' ', re.sub('\.', '. ', s)))
    if res[-1] != '.':
       res += '.'
    return res

>>> print correction(s)
This is weird. Indeed.

>>> s=raw_input()
hee   ss.dk
>>> s
'hee   ss.dk'
>>> correction(s)
'hee ss. dk.'

Upvotes: 0

TigerhawkT3
TigerhawkT3

Reputation: 49330

For a more basic answer, without regex:

>>> def remove_doublespace(string):
...     if '  ' not in string:
...         return string
...     return remove_doublespace(string.replace('  ',' '))
...
>>> remove_doublespace('hi there  how    are   you.i am fine. '.replace('.', '. '))
'hi there how are you. i am fine. '

Upvotes: 0

Related Questions