split() and map() Functions to take Multiple Inputs

05 min read

Split() function in Python helps to cut the given long string into pieces at specified intervals. 

By default split() divided a given string at the " " (spaces). For example,

str1 = "Focus Academy Headquarters is situated in Coimbatore".
str1.split()

Output:

[ 'Focus' , 'Academy', 'Headquarters', 'is', 'situated', 'in' , 'Coimbatore' ]

str2 = "Focus academy : Placements : FACE_Prep : Codelab"
str2.split(:)

Output:

['Focus Academy' , 'Placements' , 'FACE_Prep' , 'Codelab']

The split function is mostly used along with map function to take multiple inputs from the user and simultaneously alot variables to them. We can see the following example to comprehend the best use of split and map functions.

Consider an example of input, where the first value represents the number of test cases, and in each of the test case there are 4 values that need to be used in a specific program.

def programs:
      body of the program
.
       .
.
.

t= map(int, input().strip()) # No of test cases.
for a0 in range(t): # run loop to take each test case
N = list(map(int, input().strip().split())) # each input value is then splitted based on spaces,
#and each value is cleaned by stripping spaces next to it.
#Final value is mapped to int datatype and stored in the created list
Results = programs(N)
print(Results)
POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error