Is there a method that returns a character at a specified index?

Alex Martelli aleax at aleax.it
Mon Oct 27 06:38:03 EST 2003


Michael Loomington wrote:

> Couldn't find anything here
> http://www.python.org/doc/2.3.2/lib/string-methods.html
> 
> I need to cycle through lines in a file that contain words and compare it
> to a string thats given, call it SSS to see if the words in the file can
> be
> written from SSS.  I am planning to grab each letter from the word one at
> a time and removing it from SSS and if the letter doesn't exist then I
> will go
> on to the next word.  That's why I need a method that returns a letter at
> each index.  Maybe there an easier way of doing this.

Absolutely -- much faster.  Check out the string.maketrans function
in module string, and the .translate method of string instances.  The
Python Cookbook, in discussion of recipes 3.6 and 3.7 on pp. 76-78,
has in-depth discussion and examples.


Alex





More information about the Python-list mailing list