7

I've used this method before but can't find it in any of my codes so here I am on Stack Overflow :) What I'm trying to do is split an input into two( the user is asked to enter two digits separated by space). How would you call the first digit a and the second digit b ? The code so far doesn't seem to work.

a,b= input(split" "("Please enter two digits separated by space"))
dkentre
  • 289
  • 3
  • 5
  • 10
  • 4
    Consider attempting to locate relevant documentation before posting syntactically invalid code in a question. To wit: this is really basic stuff; please make a concerted effort. – Matt Ball Sep 28 '13 at 04:10
  • 2
    -1 for lack of research effort. There are tonnes of dupes for this. – Games Brainiac Sep 28 '13 at 04:15
  • 1
    possible duplicate of [Python: split on either a space or a hyphen?](http://stackoverflow.com/questions/16926870/python-split-on-either-a-space-or-a-hyphen) – Games Brainiac Sep 28 '13 at 04:16
  • 2
    Also downvote for showing zero motivation for doing basic research –  Sep 28 '13 at 06:17

2 Answers2

12

You're calling the function wrongly.

>>> "hello world".split()
['hello', 'world']

split slits a string by a space by default, but you can change this behavior:

>>> "hello, world".split(',')
['hello', ' world']

In your case:

a,b= input("Please enter two digits separated by space").split()
Games Brainiac
  • 80,178
  • 33
  • 141
  • 199
5

The str.split() function is an attribute of the type str (string). To call it, you do:

input("Please enter two digits separated by space").split()

Note that .split(" ") isn't needed as that is what it is by default.

TerryA
  • 58,805
  • 11
  • 114
  • 143