Replace spaces with commas python

I have a list of items which look like so:

 2.4       -2.0           4.3
-6.0       12.5           1.0

What I would like is to remove all those spaces and replace them with "," [comma] except for the spaces in front of first numbers [they should be just deleted [the spaces] and not replaced with anything]. So the upper string items should look like so, after replacement:

2.4,-2.0,4.3
-6.0,12.5,1.0

Not like this:

,2.4,-2.0,4.3
,-6.0,12.5,1.0

Which is what the following code does:

newStrings = []
for s in strings:
    newStrings.append[re.sub['\s+', ',', s]]

What regular expression for re.sub should be used to achieve that? Thank you.

Table of Contents

  • How to replace comma with space in list in Python
    • Using the replace[] function to replace comma with space in list in Python
    • Using the re.sub[] function to replace comma with space in list in Python
    • Using the split[] and join[] functions to replace comma with space in list in Python.
  • Conclusion
    • Was this post helpful?

In this post, we will see how to replace comma with space in the list.

A list is one of the fundamental collection objects in Python. It can store multiple elements and we can access these elements using their respective index. When we display a list, every element is separated with a comma.

In this article, we will discuss how to replace comma with space in list in Python. Note that we cannot directly replace the comma while displaying a list. So in our methods, we will convert the list to a string and then replace the comma with space in list in Python.

The methods are discussed below.

Using the replace[] function to replace comma with space in list in Python

In Python, we can replace substrings within a string using the replace[] function. Using this function, we can replace comma with space in list in Python. It returns a new string with the substituted substring.

But first, remember that we need to get the string representation of the list. We can achieve this using the str[] function.

See the code below.

lst=[11,33,22,99]

s=str[lst]

s=s.replace[',',' ']

print[s]    

Output:

[11 33 22 99]

In the above example, we replace comma with space in list in Python. We first converted the list to a string using the str[] function. Then we replaced the commas with space using the replace[] function.

Using the re.sub[] function to replace comma with space in list in Python

Regular expressions are an elegant way to match parts of strings using regex patterns. In Python, we can use the re module to work with regular expressions.

The re.sub[] function can be used to replace a given substring that matches some regex pattern. We can use it to replace comma with space in list in Python. For our case, we use it without any regex pattern and directly mention commas in the function.

See the code below.

import re

lst=[11,33,22,99]

s=str[lst]

s= re.sub[',',' ',s]

print[s]    

Output:

[11 33 22 99]

Using the split[] and join[] functions to replace comma with space in list in Python.

The split[] function is used to split a string into a list of substrings by splitting it based on a character. The join[] function is used in Python to combine the elements from an iterable as a string.

First, we will convert the list to a string as done in previous methods. We will then split the string based on the comma using the split[] function. Then the elements are combined using the join[] function. The space is used as a separator character in the join[] function.

See the code below.

import re

lst=[11,33,22,99]

s=str[lst]

s= ''.join[iforiins.split[',']]

print[s]    

Output:

[11 33 22 99]

In the above example, we first split the string to a list and then combine it again to replace comma with space in list in Python.

Conclusion

To conclude, we discussed how to replace comma with space in list in Python. The first two methods were very straightforward.

We converted the list to a string and replaced the comma with space using the replace[] and re.sub[] functions.

In the final method, we used two functions to split the string into a list again and then use the join[] function to combine the elements using the space character as the separator.

That’s all about how to replace comma with space in List in Python

How do you replace a space with a comma in Python?

Using the replace[] function to replace comma with space in list in Python. In Python, we can replace substrings within a string using the replace[] function. Using this function, we can replace comma with space in list in Python. It returns a new string with the substituted substring.

How do you replace space with commas?

Open the Replace dialog Ctrl + H..
Type in a single space character, in the Find what: zone..
Type in a , character, in the Replace with: zone..
Select the Normal search mode..
Click on the Replace All button..

How do I change the separator in Python?

How to change the delimiter in a CSV file.
Create a new Python file in the location where your CSV file is saved. ... .
Open up an Anaconda Prompt instance. ... .
Type python change_delimiter.py [replacing change_delimiter.py with the name of your Python file] then press Enter..

How do I remove commas and spaces in a list in Python?

Use str. replace[] to remove a comma from a string in Python Call str. replace[',', ''] to replace every instance of a ',' in str with '' .

Chủ Đề