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".
[ 'Focus' , 'Academy', 'Headquarters', 'is', 'situated', 'in' , 'Coimbatore' ]
str2 = "Focus academy : Placements : FACE_Prep : Codelab"
['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.
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)