Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #52495
| References | <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> |
|---|---|
| Date | 2013-08-13 22:37 -0700 |
| Subject | Re: .split() Qeustion |
| From | Krishnan Shankar <i.am.songoku@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.562.1376459009.1251.python-list@python.org> (permalink) |
[Multipart message — attachments visible in raw view] - view raw
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@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 >
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
.split() Qeustion eschneider92@comcast.net - 2013-08-13 21:51 -0700
Re: .split() Qeustion Gary Herron <gary.herron@islandtraining.com> - 2013-08-13 22:12 -0700
Re: .split() Qeustion Alister <alister.ware@ntlworld.com> - 2013-08-14 08:30 +0000
Re: .split() Qeustion Joshua Landau <joshua@landau.ws> - 2013-08-14 11:31 +0100
Re: .split() Qeustion Alister <alister.ware@ntlworld.com> - 2013-08-14 13:29 +0000
Re: .split() Qeustion Duncan Booth <duncan.booth@invalid.invalid> - 2013-08-15 09:15 +0000
Re: .split() Qeustion wxjmfauth@gmail.com - 2013-08-15 02:46 -0700
Re: .split() Qeustion Chris Angelico <rosuav@gmail.com> - 2013-08-15 10:54 +0100
Re: .split() Qeustion Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-15 11:22 +0000
Re: .split() Qeustion wxjmfauth@gmail.com - 2013-08-15 06:58 -0700
Re: .split() Qeustion Peter Otten <__peter__@web.de> - 2013-08-14 13:45 +0200
Re: .split() Qeustion Joshua Landau <joshua@landau.ws> - 2013-08-14 12:55 +0100
Re: .split() Qeustion wxjmfauth@gmail.com - 2013-08-14 07:32 -0700
Re: .split() Qeustion random832@fastmail.us - 2013-08-14 13:05 -0400
Re: .split() Qeustion Steven D'Aprano <steve@pearwood.info> - 2013-08-15 07:17 +0000
Re: .split() Qeustion Chris Angelico <rosuav@gmail.com> - 2013-08-14 18:14 +0100
Re: .split() Qeustion wxjmfauth@gmail.com - 2013-08-15 00:46 -0700
Re: .split() Qeustion Lele Gaifax <lele@metapensiero.it> - 2013-08-15 16:38 +0200
Re: .split() Qeustion MRAB <python@mrabarnett.plus.com> - 2013-08-15 15:54 +0100
Re: .split() Qeustion Lele Gaifax <lele@metapensiero.it> - 2013-08-15 17:30 +0200
Re: .split() Qeustion Chris Angelico <rosuav@gmail.com> - 2013-08-15 16:43 +0100
Re: .split() Qeustion Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-16 04:13 +0000
Re: .split() Qeustion Roy Smith <roy@panix.com> - 2013-08-16 00:29 -0400
Re: .split() Qeustion Dave Angel <davea@davea.name> - 2013-08-16 05:27 +0000
Re: .split() Qeustion Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-17 02:38 +0000
Re: .split() Qeustion Chris Angelico <rosuav@gmail.com> - 2013-08-17 03:45 +0100
Re: .split() Qeustion Gene Heskett <gheskett@wdtv.com> - 2013-08-16 10:30 -0400
Re: .split() Qeustion Gene Heskett <gheskett@wdtv.com> - 2013-08-16 10:24 -0400
Re: .split() Qeustion Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2013-08-17 11:16 +1200
Re: .split() Qeustion Ben Finney <ben+python@benfinney.id.au> - 2013-08-16 15:59 +1000
Re: .split() Qeustion Roy Smith <roy@panix.com> - 2013-08-16 07:14 -0400
Re: .split() Qeustion wxjmfauth@gmail.com - 2013-08-16 06:14 -0700
Re: .split() Qeustion Roy Smith <roy@panix.com> - 2013-08-16 09:23 -0400
Re: .split() Qeustion wxjmfauth@gmail.com - 2013-08-17 01:09 -0700
Re: .split() Qeustion Roy Smith <roy@panix.com> - 2013-08-17 07:55 -0400
Re: .split() Qeustion Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2013-08-18 11:30 +1200
Re: .split() Qeustion wxjmfauth@gmail.com - 2013-08-18 00:17 -0700
Re: .split() Qeustion Grant Edwards <invalid@invalid.invalid> - 2013-08-16 13:59 +0000
Re: .split() Qeustion Joshua Landau <joshua@landau.ws> - 2013-08-15 17:54 +0100
Re: .split() Qeustion Chris Angelico <rosuav@gmail.com> - 2013-08-15 19:28 +0100
Re: .split() Qeustion Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-16 04:17 +0000
Re: .split() Qeustion Joshua Landau <joshua@landau.ws> - 2013-08-15 19:40 +0100
Re: .split() Qeustion Terry Reedy <tjreedy@udel.edu> - 2013-08-15 17:40 -0400
Re: .split() Qeustion Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-16 04:22 +0000
Re: .split() Qeustion Dave Angel <davea@davea.name> - 2013-08-15 22:56 +0000
Re: .split() Qeustion Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-16 04:39 +0000
Re: .split() Qeustion Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-16 04:41 +0000
Re: .split() Qeustion Tim Chase <python.list@tim.thechases.com> - 2013-08-14 12:29 -0500
Re: .split() Qeustion Skip Montanaro <skip@pobox.com> - 2013-08-14 12:38 -0500
Re: .split() Qeustion Chris Angelico <rosuav@gmail.com> - 2013-08-14 18:46 +0100
Re: .split() Qeustion Terry Reedy <tjreedy@udel.edu> - 2013-08-14 15:45 -0400
Re: .split() Qeustion Dave Angel <davea@davea.name> - 2013-08-14 05:35 +0000
Re: .split() Qeustion eschneider92@comcast.net - 2013-08-13 22:44 -0700
Re: .split() Qeustion Krishnan Shankar <i.am.songoku@gmail.com> - 2013-08-13 22:37 -0700
.split() Qeustion "Alfonso Andalon Jr." <alfonsoandalon@gmail.com> - 2013-08-16 21:31 -0700
csiph-web