A Python newbie ask a simple question

Wayne Brehaut wbrehaut at mcsnet.ca
Fri Jul 13 19:47:28 EDT 2007


On Fri, 13 Jul 2007 14:51:52 -0400, "Jeff McNeil" <jeff at jmcneil.net>
wrote:

>The raw_input built-in returns a string.  The '[0]' subscript returns
>the first character in the user supplied response as strings support
>indexing.
>
>[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
>Type "help", "copyright", "credits" or "license" for more information.
>>>> mystr = "asdf"
>>>> mystr[0]
>'a'
>>>>
>
>>>> raw_input("Another one, please: ")[0]
>Another one, please: ASDF
>'A'
>>>>
>
>-Jeff

And, as I'm sure Jeff intended to add, you should always try to answer
such questions for yourself by just trying a few examples.  An
advantage of Python is that it's very quick and easy to test one idea
at a time without having to create a complete program skeleton before
getting started as you would have to in some other languages (such as
Java).

It also wouldn't hurt to start with one of the many introductory
tutorials, some referenced in similar threads above in this group
under topics like:

	Off Topic: What is the good book to learn Python ?
	Want to learn Python
	Start

to see what others recommend you start with.  Or just take a common
suggestion and go to:

	http://www.python.org/doc/

and check out some of the beginners' resources listed and linked to
there, such as the "offical" tutorial:

	http://docs.python.org/tut/tut.html

or the widely used wiki site:

	http://wiki.python.org/moin/BeginnersGuide

or the list of Introductory Books:

	http://wiki.python.org/moin/IntroductoryBooks

It may be that none of these answer the specific question you have,
but if you start with some of these you'll possibly get a more
efficient and enjoyable start than trying to find details of one
particular example--and you'll then also have a number of resources at
hand with which to try to find your own answers.  

In case you still have questions, you can always ask in this friendly
group; though perhaps more appropriate for "getting started" questions
might be the Python Tutorial list at:

	http://mail.python.org/mailman/listinfo/tutor

I use ActivePython a lot, (from ActiveState at
http://aspn.activestate.com/ASPN/Python/Downloads/), and use Help,
Python Manuals, Python Documentation instead of having to store, open,
and maintain the most commonly used dfocumentation separately.  Note
that the Tutorial is listed here, so it's conveniently located and
easy to copy code and paste into the interactive window to test! Or
you can use the usual Help Index facility to find the description of
"raw_input":

===
raw_input( [prompt]) 

If the prompt argument is present, it is written to standard output
without a trailing newline. The function then reads a line from input,
converts it to a string (stripping a trailing newline), and returns
that. When EOF is read, EOFError is raised. Example: 
===

Knowing this, and having checked out some of the tutorials so you know
that [0] is the first element of any "indexable object" (sequence); or
using the Help Index again with "sequence" (since you will probably
have noted from your tutorial exercises  that strings are one type of
sequence) you find that one of the operations on strings is:

	s[i]	 i'th item of s, origin 0 

so the answer to your question:

	What does the statement "choice = raw_input(prompt)[0]" mean?

is now obvious.  And the more tutorials you work through, and the more
examples you try, the more such statements will be "obvious" or easy
to figure out!

Happy pythoning!

wwwayne

>On 7/13/07, xing93111 at gmail.com <xing93111 at gmail.com> wrote:
>> what does the statement "choice = raw_input(prompt)[0]" mean? I don't
>> know why there is a '[0]' in the statement.
>>
>> Thank you very much
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>



More information about the Python-list mailing list