Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split an input into two. Python 3 [closed]

Tags:

python

split

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"))
like image 336
dkentre Avatar asked Sep 28 '13 04:09

dkentre


People also ask

How to split a list in Python with multiple input?

Generally, user use a split () method to split a Python string but one can use it in taking multiple input. List comprehension is an elegant way to define and create list in Python.

How to get multiple inputs from a string in Python?

This function helps in getting a multiple inputs from user. It breaks the given input by the specified separator. If a separator is not provided then any white space is a separator. Generally, user use a split () method to split a Python string but one can use it in taking multiple input.

What is the use of split method in Python?

Definition and Usage. The split () method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.

How do I split a string between characters in Python?

The Python standard library comes with a function for splitting strings: the split () function. This function can be used to split strings between characters. The split () function takes two parameters. The first is called the separator and it determines which character is used to split the string.


1 Answers

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()
like image 175
Games Brainiac Avatar answered Oct 12 '22 04:10

Games Brainiac