Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!us.feeder.erje.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'argument': 0.05; 'item,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'random': 0.14; '(am': 0.16; 'character.': 0.16; 'cherry': 0.16; 'code?': 0.16; 'comma': 0.16; 'email addr:comcast.net': 0.16; 'optional': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'separator,': 0.16; 'whitespace.': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'split': 0.19; 'typing': 0.19; 'import': 0.22; 'header:User- Agent:1': 0.23; 'specify': 0.24; 'class.': 0.26; 'header:X -Complaints-To:1': 0.27; 'words': 0.29; 'list:': 0.30; 'file': 0.32; 'skip:m 30': 0.32; 'guess': 0.33; 'sense': 0.34; 'maybe': 0.34; "can't": 0.35; 'but': 0.35; 'there': 0.35; 'method': 0.36; 'charset:us-ascii': 0.36; 'list': 0.37; 'thank': 0.38; 'apple': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'called': 0.40; 'how': 0.40; "you're": 0.61; 'first': 0.61; 'you.': 0.62; 'series': 0.66; 'between': 0.67; 'default': 0.69; 'pie': 0.84; 'items,': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dave Angel Subject: Re: .split() Qeustion Date: Wed, 14 Aug 2013 05:35:40 +0000 (UTC) References: <94f8428f-50b9-4ccd-95a0-6eeafda0fe18@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 174.32.174.30 User-Agent: XPN/1.2.6 (Street Spirit ; Linux) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1376458565 news.xs4all.nl 15864 [2001:888:2000:d::a6]:57824 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52493 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)) > I can't make any sense out of the first sentence. But maybe I can guess what you're looking for. The split() method is indeed a method of the str class. It takes an optional argument for the separator character. By default it uses whitespace. So if you're trying to specify a series of items, none of which contain any whitespace, you can readily use split to build your list: pie = "keylime peach apple cherry pecan".split() However, there'd be no way to specify an item called "chocolate marshmallow". If you need to include whitespace in any item, then you'd have to use some other separator, like a comma: pie = "keylime,chocolate marshmallow,peach,apple,cherry, pecan".split(",") -- Signature file not found