Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read formatted input in python?

Tags:

python

input

I want to read from stdin five numbers entered as follows:

3, 4, 5, 1, 8

into seperate variables a,b,c,d & e.

How do I do this in python?

I tried this:

import string
a=input()
b=a.split(', ')

for two integers, but it does not work. I get:

Traceback (most recent call last):
  File "C:\Users\Desktop\comb.py", line 3, in <module>
    b=a.split(', ')
AttributeError: 'tuple' object has no attribute 'split'

How to do this? and suppose I have not a fixed but a variable number n integers. Then?

like image 367
Lazer Avatar asked Sep 09 '09 06:09

Lazer


People also ask

How do you get the formatted input in Python?

Another way to format strings in Python is to use the format() method. In this method, first, we put placeholders in the string in which the variables are to be inserted. Then we invoke the format method on the string with variables that have to be inserted into the string as input arguments.

How do you read user input in Python?

Python user input from the keyboard can be read using the input() built-in function. The input from the user is read as a string and can be assigned to a variable. After entering the value from the keyboard, we have to press the “Enter” button. Then the input() function reads the value entered by the user.

What is %s and %D Python?

%s is used as a placeholder for string values you want to inject into a formatted string. %d is used as a placeholder for numeric or decimal values. For example (for python 3) print ('%s is %d years old' % ('Joe', 42)) Would output Joe is 42 years old.


2 Answers

Use raw_input() instead of input().

# Python 2.5.4
>>> a = raw_input()
3, 4, 5
>>> a
'3, 4, 5'
>>> b = a.split(', ')
>>> b
['3', '4', '5']
>>> [s.strip() for s in raw_input().split(",")] # one liner
3, 4, 5
['3', '4', '5']

The misleadingly names input function does not do what you'd expect it to. It actually evaluates the input from stdin as python code.

In your case it turns out that what you then have is a tuple of numbers in a, all parsed and ready for work, but generally you don't really want to use this curious side effect. Other inputs can cause any number of things to happen.

Incidentally, in Python 3 they fixed this, and now the input function does what you'd expect.

Two more things:

  1. You don't need to import string to do simple string manipulations.
  2. Like mjv said, to split a tuple or a list into several variables, you can 'unpack' it. This will not be feasible if you don't know how long the list will be, though.

Unpacking:

>>> l = (1,2,3,4,5)
>>> a,b,c,d,e = l
>>> e
5
like image 88
itsadok Avatar answered Oct 23 '22 09:10

itsadok


Under Python 2.x only (*)

after a = input()

a is a tuple with the 5 values readily parsed!

A quick way to assign these 5 values is

a, b, c, d, e = a

(*) with Python version 3.0? or for sure 3.1, input() works more like the raw_input() method of 2.x, which makes it less confusing. (thank you mshsayem to point this out!)

like image 37
mjv Avatar answered Oct 23 '22 11:10

mjv