Some basic questions
Irmen de Jong
irmen at -NOSPAM-REMOVE-THIS-xs4all.nl
Wed Mar 26 16:02:37 EST 2003
Brian Christopher Robinson wrote:
> I just began writing Python today and have a few basic questions:
>
> How can I read a single character from the standard input without consuming
> any more input?
There is only a platform specific way to do this, AFAIK.
Look in the cookbook:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/134892
> Is there any way to write a boolean function? I wanted to write a simple
> is_alpha function (which may be in the standard library but I'm leanring),
> and this is what I came up with:
>
> def is_alpha(c) :
> letters = re.compile("[a-zA-Z]")
> return letters.match(c)
>
> Instead of checking for true or false I check to see if None is returned,
> but that seems a little kludgy.
Just use a boolean expression in your return statement, in your example
this would be:
def is_alpha(c) :
letters = re.compile("[a-zA-Z]")
return letters.match(c) is not None
(Note that recompiling the regex on each function invocation isn't
very efficient, but that's another story).
> How can I print to standard out without printing a newline? It seems that
> the print statement always adds a newline and I haven't found any other
> outputting functions yet.
Use a comma at the end of your print statement, like:
print "blabla",
Or use the file object sys.stdout:
sys.stdout.write("blabla")
--Irmen
More information about the Python-list
mailing list