How to add space after dot or comma in Python string

We are going to learn about adding space after dot or comma in a string, using Python programming language in this tutorial.  It is used in many cases like while making software, web applications, games, and so on.

Here, we are going to solve this problem using two different ways. First, we will learn using replace() method, and the second one using Regular Expression(re).
You can go through with the link below; if you have no idea about Regular Expressions.
Python Regular Expression

Using Replace() method

Python has an inbuilt function called replace(), which replaces all the occurrences of a substring with a new string. We use it to replace a character or sub-string with another character or substring from a string.

Syntax:

replace('character to replace','replacement',count)

In replace() function, the first parameter takes the character or string to be replaced, the second one is replacement i.e. new character or string to replace the old ones and the last one count is optional, which represents the number of times to replace the character or string.

To add space after dot or comma using replace() in Python

As input, we take a string containing no space after dot or comma in the ‘string’ variable. Then using replace(), we will replace comma ‘,’ or ‘.’ with ‘, ‘ or ‘. ‘(comma or dot with a space) and store it in ‘s’ variable. Finally, it will print the output by adding space after the dot or comma in the string.

string = "A text without space,after comma,and dot.To test,the output,in CodeSpeedy."
s = string.replace(",",", ").replace(".",". ")
print(s)

Output:

A text without space, after comma, and dot. To test, the output, in CodeSpeedy.

 

Using Regular Expression (re)

Python has an inbuilt package ‘re’ to work with regular expression. The module re is used to check specified search patterns from a string. We need to import the re module at first in Python for this.
We are using here re. sub() function, where sub represents sub-string.

Syntax:

re.sub(pattern, replace, string, count=0, flags=0)

Here if the pattern is not found, the string will not be changed. It returns the string, which replaces the matches with the given sub-string. In re. sub() parameters, Pattern is the regular expression, Replace is the sub-string patterns to be replaced with, String is where the pattern to be searched from, and the Count checks the number of time occurrences. The Count and Flags (last two) parameters are optional.

To add space after dot or comma using regular expression (re):

import re
string = "A text without space,after comma,and dot.To test,the output,in CodeSpeedy."
s = re.sub(r'(?<=[.,])(?=[^\s])', r' ', string)
print(s)

Output:

A text without space, after comma, and dot. To test, the output, in CodeSpeedy.

In the above code, first, we will import re, then take a text input in the ‘string’ variable. Now in re. sub(), (?<=[.,]) is a positive look behind which searches for a dot or comma, and (?=[^\s]) is a positive lookahead which matches anything except space. We are storing this in ‘s’ variable and printing the output which adds space after the dot or comma in the string.

I hope you will find this tutorial helpful.

Thanks for your time.

Leave a Reply

Your email address will not be published. Required fields are marked *