The input consists of three space separated integers in python

Use input(), map() and split() function to take space-separated integer input in Python 3. You have to use list() to convert the map to a list.

list(map(int,input().split())) 

Where:

  • input() accepts a string from STDIN.
  • split() splits the string about whitespace character and returns a list of strings.
  • map() passes each element of the 2nd argument to the first argument and returns a map object

Simple example code stage user multiple integers input, each separated space.

print("Enter the numbers: ")

inp = list(map(int, input().split()))

print(inp)

Output:

The input consists of three space separated integers in python

Do comment if you have any doubts and suggestions on this Python input program.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

The input consists of three space separated integers in python

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.

So you started competitive programming, but having difficulty in taking input and formatting output even in the simplest ones? No problem you are at the right place, this post will tell you the most important input and output techniques used in competitive programming in python.

Photo by Fabian Grohs on Unsplash

Input

Most of the problems require you to take inputs of different kinds so here are some of the important techniques

1. Taking single input

If you want to take only one input then you only need to use the input() function and then you can convert it to the type you want. For eg-

For strings use n = input()

For integers use n = int(input())

For floating-point or decimal numbers use n=float(input())

2. Taking a known number of inputs

If you want to take a fixed number of space-separated inputs (for eg- dimensions of a matrix) then you use the following technique

For strings m,n = input().split()

This will assign one of the two space-separated strings to both m and n respectively.

For integers and floats m,n = map(int,input().split()) or m,n = map(float,input().split())

As we know that input().split() returns an iterable so what we do is convert each of the objects to integer using map() which applies a function (int() in our case) to every object of the iterable.

If you have two arguments but you don’t want two waste a variable for one of them then you can use _ in its place for eg-

You want to take only first and the third integer in the variable m and n, in the example given below then you would use the following piece of code-

1 2 3m,_,n = map(int,input().split())

3. Taking a variable number of inputs

For taking a variable number of space-separated inputs we usually assign them to a list but you could also use set or tuple according to your requirement.

For strings -

I love pythonl = list(input().split())

The value of l will be l=['I','love','python']

For integers or floats -

1 2 3 4 5 6 7l = list(map(int,input().split())

The value of l will be l = [1,2,3,4,5,6,7]

If you want sets or tuples then use tuple() or set() instead of list().

4. Taking fix and a variable number of inputs

If you want to store the starting values in variables and remaining values in a list then we use * consider the following examples

hello 1 2 3 4s,*l = input().split()
l = list(map(int,l))

We would get s='hello' and l=[1,2,3,4]

A little bit more complex example would be

hello 3 1 2 3_,n,*l = input().split()
n = int(n)
l = list(map(int,l))

We would get n=3 and l=[1,2,3].

I hope by now you have got the idea of taking any kind of space-separated input in python.

Output

Outputting the data wouldn’t be that difficult, the only possible cases are

1. Outputting on different lines

Almost 90% of problems would require you to output this way. Its Very simple consider the following example -

result = [1,2,3,4,5]
for i in result:
print(i)

The output will be-

1
2
3
4
5

2. Outputting in the same line

If you want to output the result in the same line then you will have to use the end attribute of print()

result = [1,2,3,4,5]
for i in result:
print(i, end=' ')

The output will be 1 2 3 4 5

3. Advanced outputting techniques

Some problems like Google’s codejam and kickstart require you to mention case number with the output for that we use a C like technique to print the output.

For eg -

result = [15,23,32]
for i in range(len(result)):
print("Case #{}: {}".format(i+1, result[i]))

This would give the following output:

Case #1: 15
Case #2: 23
Case #3: 32

What’s happening here is the {} is acting as a placeholder for a variable that is provided in format() just like %d in C. Here are some more examples-

print("I love {}".format("python"))l=['python','is','the','best','language']
print("I love python because {}".format(' '.join(l)))

Output-

I love python
I love python because python is the best language

By now I hope you have understood all the input and output techniques used in competitive programming in python.

If you want to see some solved problems on Hackerearth and Hackerrank then here are the links to the github repositories of some of my solved problems on both platforms (almost all solutions have a link to the problem commented out in the first line).

How do you input 3 space

Use input(), map() and split() function to take space-separated integer input in Python 3.

How do you take a space

Use an input() function to accept the list elements from a user in the format of a string separated by space. Next, use a split() function to split an input string by space. The split() method splits a string into a list.

How do you write n space

Thanks. Easy solution is just to use the string method split. input: 5 8 0 sgshsu 123 input. split(" ") == ["5", "8", "0", "sgshsu", "123"] #Then they are easy to convert to numeric datatypes, but invalid inputs might raise errors.

How do you take inputs of space

Procedure:.
Using readline() method of BufferedReader and Scan the whole string..
Split this String for str. split(” “).
Iterate over the above array and parse each integer value using Integer. parseInt( )..