Thread: Str.split

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Str.split


    hey,
    could somebody please explain to me about the function "split"? (on strings)

    what is exactly the role of the secend and the third arguments?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,850
    Rep Power
    481
    I had no idea there were second and third arguments to split....mmm...a third argument anyway. OK, you deserve a real answer not mere snot. I originally intended to say "Try the *!$# thing."

    Suppose you use the method directly from a class, as in

    str.split

    Well, it's unbound. There isn't an associated string object. So the first argument needs to be the string to be split.
    The second argument is the string used to split the string object.
    Code:
    assert str.split('theABcatABate my ABcdefgABdog','AB') == ['the', 'cat', 'ate my ', 'cdefg', 'dog']
    Apparently there's an optional final integral argument to limit the number of cuts for your experimental fulfillment.

    But suppose you use OBJECT.method
    In this case the method is bound to the OBJECT, silently passed as the first parameter.
    Code:
    assert 'theABcatABate my ABcdefgABdog'.split('AB') == ['the', 'cat', 'ate my ', 'cdefg', 'dog']
    Bound versus unbound method call misunderstanding frequently explains programmer's mystification about the strange argument count. Remember, you said str.split takes 3 arguments.
    Code:
    >>> 'the'.split('blort', 'h', 8)  # HUH? 2 arguments?  I thought there were 3.
    Traceback (most recent call last):
      File "p.py", line 3, in <module>
        'the'.split('blort', 'h', 8)
    TypeError: split() takes at most 2 arguments (3 given)
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Thank you so much!


    Thanks

IMN logo majestic logo threadwatch logo seochat tools logo