Reputation: 21
I need to remove extra spaces in a sentences. The sentence is "I like python"
and I need to make "I like python"
. So I used replace like this:
>>>
>>> sentence="I like python"
>>> def checkio(element):
newelement=element.replace(" ", " ") and element.replace(" ", " ")
return newelement
>>> checkio(sentence)
'I like python'
>>>
And you see, the result is "I like python"
even though I (think) I told it to replace " "
with " "
. Can someone clear it up for me why this didn't work?
Upvotes: 2
Views: 641
Reputation: 48725
This is probably what you wanted to do:
def checkio(element):
newelement = element.replace(" ", " ").replace(" ", " ")
return newelement
You could even shorten this to
def checkio(element):
return element.replace(" ", " ").replace(" ", " ")
However, what if you wanted to replace an arbitrary number of spaces? It is better to split the sentence on any number of spaces, and then join it back together with a single space. To do this, you would use
def checkio(element):
return " ".join(element.split())
The split
method takes a string and separates it into a list. With no argument, it separates on spaces:
>>> "I like python".split()
["I", "like", "python"]
The join
method takes a list and makes a string out of it, using the string in between the elements of the list:
>>> ' '.join(["I", "like", "python"])
"I like python"
>>> '__HELP__'.join(["I", "like", "python"])
"I__HELP__like__HELP__python"
Why your original attempt failed
The line
newelement = element.replace(" ", " ") and element.replace(" ", " ")
was not doing what you expect because it was using the "truthiness" of strings. The and
operator looks at logical things. In python, all strings except ""
are considered True
.
The you can use this to do something called "short-circuiting", which is what you did.
When you use and
, both the left and right statement must be true for it to evaluate to True
. If the left part is False
, there is no reason to move on to the right part and evaluation stops there (aka "short-circuiting"). If it is true, then it needs to check the right side to see if the whole statement is true. You can exploit this fact to return the left statement if it is false, or right if the left is true:
>>> a = "a" and "b"
>>> a
"b"
>>> a = "" and "b"
>>> a
""
>>> # It works oppositely with "or"
>>> a = "a" or "b"
>>> a
"a"
>>> a = "" or "b"
>>> a
"b"
element.replace(" ", " ")
returns a string, and so does element.replace(" ", " ")
. Since the strings evaluate to True
, you are essentially only ever returning the right statement of the and
.
This is not a common use of logical operatiors in python (it is more often used in other languages, though)
Upvotes: 3
Reputation: 27802
How about using this to remove extra whitespace:
" ".join(sentence.split())
If you want, you can package this as a function:
def checkio(element):
return " ".join(element.split())
and
statement.Python first evaluates element.replace(" ", " ")
, which gives "I like python"
, a True
statement.
Since this is true, Python continues with the next statement, and returns that value
The next statement is element.replace(" ", " ")
, which itself evaluates to "I like python"
Finally, it sets this value to newelement
, which your function returns
Upvotes: 2
Reputation: 304137
element.replace(...)
doesn't modify element
It just returns a modified version of the string
You could use
element = element.replace(' ', ' ')
newelement = element.replace(' ', ' ')
It's not a very good general solution though
str.split()
will group up any amount of whitespace for you, so
newelement = ' '.join(element.split())
is the easiest way to do this particular task. If you had to remove extra .
from "I..like...python"
, you would need another approach
Upvotes: 0
Reputation: 1817
As was mentioned in a comment, I think you understanding of "and" is misplaced.
The actual reason which people are avoiding is that the first statement actually takes 2 spaces away from each instance, and the second statement never finds any 3-spaced segments, leaving you with (" "... " ")
Therefore the 2-space + 3 space becomes a 1 space + 2 space. (Avoiding typing because spaces are hard to view here)
Upvotes: 1