I want to cut a string in two and I'm doing this.
parts = str.split("street one")
Would it be better to do this? Or more efficient?
parts = "street one".split()
I want to cut a string in two and I'm doing this.
parts = str.split("street one")
Would it be better to do this? Or more efficient?
parts = "street one".split()
Given an object obj
of type Class
, the following two notations are equivalent:
obj.method(arg)
Class.method(obj, arg)
So in your case, these two are equivalent:
"street one".split()
str.split("street one")
And these two are equivalent:
"street one".split(" ")
str.split("street one", " ")
Both are valid, and correct, Python. So the question is simply which is stylistically preferable and more "Pythonic". I would say that the second is Pythonic and should be preferred; but the first is not "wrong".
I am not aware of any performance differences between the two forms.
Both of them perform the same task of splitting.i.e-> ['street','one']. Anyway you do it, it's pretty much the same