Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace an empty string with 0, but leave it alone if not empty

Python's behavior seems inconsistent when replacing values in the case shown below (using python 3.6.5)

    >>> emptyString = '    '
    >>> emptyString.strip().replace('','0') #produces expected results
    '0'
    >>> notEmptyString = ' 50 '
    >>> notEmptyString.strip().replace('','0') #expected '50'
    '05000'
    >>> shortString = notEmptyString.strip()
    >>> shortString  #results as expected
    '50'
    >>> shortString.replace('','0') #unexpected results - expected '50'
    '05000'

This is what I'd like to see:

  • if string has a value, just strip() the leading and trailing spaces.
  • if string is empty (i.e. "") or string is just blank characters (i.e. " ") then strip it to be "" and replace "" with '0'

Example #1... string = " 10 ".... then just strip leading and trailing spaces
Example #2... string = ' ' .... then convert to '0'

I can get the results I want by other means, but I wondered if anybody understands why python produces these results.

like image 962
Joe Rytting Avatar asked Aug 13 '18 05:08

Joe Rytting


People also ask

How do you replace an empty string with a value in Python?

You can replace blank/empty values with DataFrame. replace() methods. The replace() method replaces the specified value with another specified value on a specified column or on all columns of a DataFrame; replaces every case of the specified value. Yields below output.

How do you replace a blank string in Java?

Sure enough, the following line of code returns a new String with all the blank characters removed: String newName = oldName. replaceAll(" ", ""); Note that there is a blank space between the first set of double quotes in that line, and there is no space between the second set of quotes.

Is empty string equal to none?

An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters. A null string is represented by null .


1 Answers

If s is a string, then:

s.replace(old, new)

returns a copy of s with every occurrence of the string old replaced with new, so for example:

In [7]: "abracadabra".replace("a","4")
Out[7]: '4br4c4d4br4'

As a special case, if old is the empty string, it inserts new at the start and end of the string and between every pairs of characters:

In [8]: "12345678".replace("","_")
Out[8]: '_1_2_3_4_5_6_7_8_'

The rationale is that there's a "empty string" before the first character, between each pair of characters, and after the last character, and that's what's being replaced.

So, replace isn't doing what you were thinking.

To do what you what, you can use one of the solutions already proposed, or something like this if you're feeling clever:

s.strip() or "0"
like image 52
K. A. Buhr Avatar answered Sep 18 '22 20:09

K. A. Buhr