.split() Qeustion
Krishnan Shankar
i.am.songoku at gmail.com
Wed Aug 14 01:37:51 EDT 2013
Hi,
>How can I use the '.split()' method (am I right in calling it a method?)
The .split() is a method in Python which comes as in built method for
String objects in Python. Any string defined in python will have the
ability to call this function.
>>> var = 'Hello how r u?'
>>> dir(var)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__',
'__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__',
'__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__',
'__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__',
'__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith',
'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower',
'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip',
'replace', 'rfind', 'rindex', 'rjust', 'rsplit', 'rstrip', 'split',
'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate',
'upper', 'zfill']
>>> var.split()
['Hello', 'how', 'r', 'u?']
>>>
>writing each comma between words in the pie list in the following code?
Also, is there >a way to use .split instead of typing the apostrophes?
Thank you.
>import random
>pie=['keylime', 'peach', 'apple', 'cherry', 'pecan']
>print(random.choice(pie))
If you are talking about having predefined list pie with limited elements
like above it is ok to code them straightaway with apostrophes and others
will know that it is a predefined list.
Suppose if the elements in list come as a line in a file or is a string, it
will be better to use split() method and form a list. I hope Gary has
provided the example for the same.
pie = 'keylime peach apple cherry pecan'.split()
I hope this clarifies your doubt.
Regards,
Krishnan
On Tue, Aug 13, 2013 at 9:51 PM, <eschneider92 at comcast.net> wrote:
> How can I use the '.split()' method (am I right in calling it a method?)
> without instead of writing each comma between words in the pie list in the
> following code? Also, is there a way to use .split instead of typing the
> apostrophes? Thank you.
>
> import random
> pie=['keylime', 'peach', 'apple', 'cherry', 'pecan']
> print(random.choice(pie))
>
> Eric
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130813/a3bc7fd7/attachment.html>
More information about the Python-list
mailing list