Re: A Python newbie ask a simple question



On Fri, 13 Jul 2007 14:51:52 -0400, "Jeff McNeil" <jeff@xxxxxxxxxxx>
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@xxxxxxxxx <xing93111@xxxxxxxxx> 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

.



Relevant Pages

  • Re: UTF-8 / German, Scandinavian letters - is it really this difficult?? Linux & Windows XP
    ... For string literals, with the "coding" declaration, Python will accept ... "coding" declaration to produce a Unicode object which unambiguously ... represents the sequence of characters - ie. something that can be ... > strings and/or gibberished characters in Tk GUI title? ...
    (comp.lang.python)
  • Re: Problem of function calls from map()
    ... 'lines' is a large list of strings each of which is seperated by '\t' ... usually easier to understand as a single gestalt (i.e., ... performance in Python (and other languages, too, but perhaps easier to ... map() isn't much of a win in this case. ...
    (comp.lang.python)
  • language learning vs. process
    ... I'm getting started with python and have almost zero programming experience. ... I'm finding that there are tons of tutorials on the internet -- such as the ... standard tutorial at python.org -- that tell you all about the language. ... I typed "import smtpmail" and it ran -- it ...
    (comp.lang.python)
  • Re: Seeking regex optimizer
    ... Note that the Python regex engine will consider each candidate in Paddy's solution left to right until it gets a match or reaches the end. ... This is worst-case Owhere N is the total of the lengths of all the strings in your list. ... "sx.matchyields a SRE_Match object" ... ... I would have thought the way to approach this would be a simple character-by-character tree/trie-driven lookup. ...
    (comp.lang.python)
  • Re: Python docs disappointing - group effort to hire writers?
    ... who is /learning/ the language is, quite frankly, terrifying. ... Currently Python dev seems to be more output than input, ... beautiful tutorials that start at various levels of entry. ... While your student is ...
    (comp.lang.python)