Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Denis McMahon Newsgroups: comp.lang.python Subject: Re: I am out of trial and error again Lists Date: Fri, 24 Oct 2014 23:21:43 +0000 (UTC) Organization: A noiseless patient Spider Lines: 37 Message-ID: References: <2412654f-ac5e-4678-8956-afc4e27affb3@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Fri, 24 Oct 2014 23:21:43 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="66ffcfa4470a58bcddbdcd1913f98ab4"; logging-data="23528"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+XYUe4ktHiN1PiA+lxCx2ecN3ftYBq1xk=" User-Agent: Pan/0.136 (I'm far too busy being delicious; GIT 926a150 git://git.gnome.org/pan2) Cancel-Lock: sha1:49g2z+gavNuFPWG4CZAXpGFyoUU= Xref: csiph.com comp.lang.python:79629 On Fri, 24 Oct 2014 16:58:00 -0400, Seymore4Head wrote: > I make lots of typing mistakes. It is not that. Did you see the short > example I posted? > > name="123-xyz-abc" > for x in name: > if x in range(10): > print ("Range",(x)) > if x in str(range(10)): > print ("String range",(x)) > > It doesn't throw an error but it doesn't print what you would expect. It prints exactly what I expect. Try the following: print(str(range(10)), type(str(range(10)))) print(str(list(range(10))), type(str(listr(range(10))))) In python 3, str(x) just wraps x up and puts it in a string. range(x) generates an iterable range object. hence str(range(10)) is a string telling you that range(10) is an iterable range object with certain boundaries. However, list(iterable) expands the iterable to the full list of possible values, so str(list(range(10))) is a string representation of the list containing the values that the iterable range(10) creates. Note that whether you're looking at a string representation of a value or the value itself is a lot clearer in the interpreter console where strings are displayed with quotes. -- Denis McMahon, denismfmcmahon@gmail.com