[Tutor] Getting the type of a variable

Alan Gauld alan.gauld at btinternet.com
Sat Oct 28 01:00:45 CEST 2006


"Etrade Griffiths" <etrade.griffiths at dsl.pipex.com> wrote
>
> I want to check the type of a variable so that I know which format 
> to use
> for printing eg

That's sometimes necessary but you may find that just
using %s will suffice.

print "%s\n" % 42
print "%s\n" % "42"
print "%s\n" % 42.0

You usually only need to check the type if the format string includes
type specific precision and justification characters. but the %s 
format
will work for most types most of the time.

> The comparison type(a) == 'int' etc does not work

type(a) == type(1)

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list