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.
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.
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.
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 .
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With