-3

How does this line exactly works in python raw_input().split() ?

2
  • have you tried googling it? Commented Dec 25, 2020 at 5:17
  • 2
    it reads the one string line as input and split it based on space Commented Dec 25, 2020 at 5:25

1 Answer 1

0

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().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.