I am out of trial and error again Lists

Denis McMahon denismfmcmahon at gmail.com
Fri Oct 24 23:19:22 CEST 2014


On Fri, 24 Oct 2014 14:15:13 -0400, Seymore4Head wrote:

> I do understand that.  7 is a number and "7" is a string.
> What my question was...and still is...is why Python 3 fails when I try
> using y=1 800 get charter
> 
> y in range str(range(10))
> should work because y is a string and str(range(10)) should be "y" in
> str(1) fails.
> It doesn't give an error it's just not True when y is a number.

This is because str(range(10)) does not do what you think it does.

In python 2.x, str(range(10)) creates a string representation of the 
complete list, not a list of the string representation of the separate 
list elements. '[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]'

In python 3.x, str(range(10)) creates a string representation of the list 
object. 'range(0, 10)'

the only single digit strings in the python3 representation are "0" and 
"1"

To recreate the python2 behaviour in python 3, use:

str(list(range(10)))

which gives

'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]'

howver the test:

if x.isdigit():

is much better.

But finally, with your telephone number decoder, look at:

http://www.codeskulptor.org/#user38_QnR06Upp4AH6h0Q.py

-- 
Denis McMahon, denismfmcmahon at gmail.com



More information about the Python-list mailing list