user3427748
user3427748

Reputation: 61

Split a string if there is space

Suppose i have set of inputs that contain integers separated by space or it may contain a single integer, Is there a way to take input in single line using raw_input().split() if space found otherwise just raw_input(). (In python 2.x)

For example :

Input : 1 2 3 4 5

In this case we can use :

     Integers=map(int,raw_input().split(' '))

Input: 2

In this case :

    Integer=int(raw_input()) 

Is there a way to combine these two in some pythonic way in one line?

Upvotes: 1

Views: 113

Answers (1)

Sukrit Kalra
Sukrit Kalra

Reputation: 34493

Use the split version, it would return a single element list when only a single integer is given to raw_input.

>>> Integers=map(int,raw_input().split())
1
>>> Integers
[1]

Upvotes: 5

Related Questions