How does this line exactly works in python raw_input().split()
?
First, input()
prompts the user for input. The user enters a series of characters that is a string. Then, the split()
function separates the string from the user into a list of words (or groups of characters) and returns list of these words or grouped characters. By default, each element in the list is separated by a blankspace. See the below examples.
words = input().split() # Typed "These are the input words"
print(words)
Output:
['These', 'are', 'the', 'input', 'words']
You can also specify a specific character other than a blank space to split on in the split()
method. For example, in this instance, the input string is split upon occurrences of a '1'
.
words = input().split('1') # Typed "100120023104"
print(words)
Output:
['', '00', '20023', '04']
Note that the split()
will omit the character requested to split the input string on (blankspace ' ' by default) in the returned list.
Also, raw_input()
is no longer supported in Python3. raw_input()
was supported by older versions of Python2. Now, we just use input()
in Python3 and it works the same way. You should upgrade your environment to Python3 if you are still using raw_input()
.