faqts : Computers : Programming : Languages : Python : Common Problems : Strings

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

15 of 15 people (100%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

how can i get user input as a string and then make it a number? i want to use the user input to control number of interations of a loop

Feb 2nd, 2002 08:02
bbeck, Christopher Arndt, Kaz Leas,


There are multiple ways (did you expect anything else? :))
1.  Use the builtin int() function
Example:
>>> s = input("Input value: ")
>>> s
'5'
>>> value = int(s)
>>> value
5
2.  Use the builtin eval() function or, even easier, the builtin input()
function.
Example:
>>> value = input("Input value: ")
Input value: 5
>>>value
5
with eval():
>>>value = raw_input("Input value: ")
Input value: 42
>>> value
'42'
>>> value = eval(value)
>>> value
42