ItSolutionStuff.com

Python String Replace Space with Comma Example

By Hardik Savani • October 30, 2023
Python

Hi Artisan,

This extensive guide will teach you python replace space with comma. If you have a question about python string replace whitespace with comma then I will give a simple example with a solution. you'll learn how to replace space with comma in python.

In this example, I will add myString variable with hello string. Then we will use replace() function to replace space with comma in python string. So, without further ado, let's see simple examples: You can use these examples with python3 (Python 3) version.

Example 1:

main.py

myString = 'It Solution Stuff com'
  
# python string replace space with comma
newString = myString.replace(' ', ',')
   
print(newString)

Output:

It,Solution,Stuff,com

Example 2:

main.py

import re
  
myString = ' 23      23          45'
  
# python string replace space with comma
newString = re.sub("\s+", ",", myString.strip())
  
print(newString)

Output:

23,23,45

Example 3:

main.py

myString = 'It,Solution,Stuff,com'
    
# python string replace comma with space
newString = myString.replace(',', ' ')
   
print(newString)

Output:

It Solution Stuff com

I hope it can help you...

Tags: Python
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Python Replace Dot with Space Example

Read Now →

Python String Replace Tab with Space Example

Read Now →

How to Replace Underscore with Space in Python?

Read Now →

How to Replace Whitespace with Underscore in Python?

Read Now →

Python List Replace Single with Double Quotes Example

Read Now →

Python List Replace Double Quotes with Single Example

Read Now →

Python String Replace Single with Double Quotes Example

Read Now →

Python String Replace Double Quotes with Single Quotes Example

Read Now →

Python String Replace All Numbers Example

Read Now →

Python String Replace Special Characters with Space Example

Read Now →

Python String Replace Multiple Spaces with Single Space Example

Read Now →

Python Replace Last Character Occurrence in String Example

Read Now →

Python Replace First Character Occurrence in String Example

Read Now →