[Tutor] How do I make Python read a string character bycharacter?

Alan Gauld alan.gauld at btinternet.com
Sat Aug 26 00:51:44 CEST 2006


"Akash" <akashmahajan at gmail.com> wrote 
>> How do I make Python read a string character by character?
> 
>>>> str = 'string'
>>>> for i in range(0, len(str)):
> ...     print str[i]
> ...

Or without indexing:

for c in "string":
  print c

> s
> t
> r
> i
> n
> g

However I'm not sure if thats what Nathan meant.

If you mean how do you read a string of characters 
from a file use read() with a size of 1.

If you mean from stdin then it depends on the platform. 
Use curses for Linux/MacOS or use msvcrt. 
In both cases the function is getch()

Alan G.



More information about the Tutor mailing list