[Tutor] passing in string from command line

Andrei Kulakov ak@silmarill.org
Thu, 06 Dec 2001 12:54:42 -0500

On Thu, Dec 06, 2001 at 05:46:07PM +0000, Kelly, Phelim wrote:
> Another Basic enough question I hope you can help with!
> I'm reading in values from the command line that I want to assign to
> variables within the code, i.e
> >>python file.py number word
> I know how to pass the typed number into a variable, i.e,
> --------------------
> try:
>    variable1 =int(sys.argv[1])
> except:
>    print "Sorry, but you did not enter a number"
>    sys.exit()
> -------------------
> What's the equivalent to "variable1 =int(sys.argv[1])" for assigning the
> entered word to a variable, something like this??????........
> "variable2 =string(sys.argv[2])"
> Thanks in advance.
> Phelim.

All the stuff in sys.argv is already in the form of strings, so you
don't need to do anything.. just var = sys.argv[2]. By the way, if you
ever have a value that's an int and you want to convert it to string,
the function is str: var = str(int_var)

> This e-mail and any attachment is for authorised use by the intended recipient(s) only.  It may contain proprietary material, confidential information and/or be subject to legal privilege.  It should not be copied, disclosed to, retained or used by, any other party.  If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender.  Thank you.
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: cy.silmarill.org