string bug/oddity?

Erno Kuusela erno-news at erno.iki.fi
Sun Aug 12 17:19:09 EDT 2001


In article <mailman.997649300.13212.python-list at python.org>, Oktay
Safak <oktay_safak at yahoo.com> writes:

| Hi everyboby,

| >>> a
| {'yas': 23, 'ad': 'veli', 'soyad': 'guzel'}
| >>> for x in a.keys():
| 	if type(a[x])!="string": print x+repr(a[x])
| 	else: print x, a[x]

| yas23
| ad'veli'
| soyad'guzel'
| >>> a
| {'yas': 23, 'ad': 'veli', 'soyad': 'guzel'}
| >>> for x in a.keys():
| 	if type(a[x])!="string": print x+repr(a[x])
| 	else: print x+a[x]
| yas23
| ad'veli'
| soyad'guzel'

type() does not return a string - it returns a type object.
so the right way to check for a string is:

if type(thingy) == types.StringType
or
if type(thingy) == type('')

also you probably want to use str() instead of repr().

  -- erno



More information about the Python-list mailing list