Add Space Before and After Specific Character Using Regex in Python



When working with text data in Python, we normally need to interact with strings to make them readable or properly formatted. It is very common way to use regular expressions or regex to add spaces before and after a particular character. In this article we will see how to do this by using Python's re module.

Adding Space at a Character Using regex

Regular expressions are a powerful way to search for and modify strings based on patterns. There are multiple uses for regex that are made possible by Python's re package.

Spaces between characters can improve the readability and the look of the text. If you need to ensure that punctuation marks or other symbols have spaces around them, regex can make formatting a website easier.

So let us see some of the examples based on it -

Example: Adding Spaces before and after " , "

In this example, we will add spaces before and after the comma (,) character in a string. We will use the re.sub() function to replace the comma with a space before and after it.

import re

# Sample string
text = "Hello,World! This is a test string."

# Add space before and after the comma
text_with_spaces = re.sub(r'\s*,\s*', ' , ', text)

# Print the modified string
print(text_with_spaces)

This will create the following outcome -

Hello , World! This is a test string.

Example: Adding Spaces before and after " ! "

Here we will add spaces before and after the exclamation mark (!) character in a string. So we will use the re.sub() function to replace the exclamation mark with a space before and after it.

import re

# Sample string
text = "Hello,World! This is a test string." 

# Add space before and after the exclamation mark
text_with_spaces = re.sub(r'\s*!+\s*', ' ! ', text)

# Print the modified string
print(text_with_spaces)

This will generate the following result -

Hello,World ! This is a test string.

Example: Adding Spaces before and after " . "

In this example, we are going to add spaces before and after the period (.) character in a string. We will use the re.sub() function to replace the period with a space before and after it.

import re
# Sample string
text = "Hello,World! This is a test string."
# Add space before and after the period
text_with_spaces = re.sub(r'\s*\.\s*', ' . ', text)
# Print the modified string
print(text_with_spaces)

This will produce the following result -

Hello,World! This is a test string .

Example: Adding Spaces before and after " - "

In this example, we will add spaces before and after the hyphen (-) character in a string. We will use the re.sub() function to replace the hyphen with a space before and after it.

import re

# Function to add space around a specific character
def add_space_around_character(text, character):
   # Replace the character with spaces before and after
   pattern = r'\s*' + re.escape(character) + r'\s*'
   return re.sub(pattern, f' {character} ', text)

# Example usage
input_text = "Hello-world! How are you?"
output_text = add_space_around_character(input_text, '-')
print(output_text)

This will lead to the following outcome -

Hello - world! How are you?
Updated on: 2025-04-24T14:45:37+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements