0

Is there a way to separate the output from the input function in Python?

For example, let's say that we want to insert a number and name.

input('Give number and name:')
Give number and name:14,John
'14,John'

We get '14,John'. Is there a way to take '14','John'?

Thanks in advance.

Joel
  • 1,544
  • 7
  • 10
  • 19
G1I2A
  • 3
  • 4

2 Answers2

0

Does this work?

user_input = input("Please write a number then a name, separated by a comma: ")

number, name = user_input.split(", ")
Aviv Shai
  • 937
  • 1
  • 9
  • 19
0

Use .split()

>>> input('Give number and name: ').split(',')
Give number and name: 14,John
['14','John']

or

>>> number, name = input('Give number and name: ').split(',')
Give number and name: 14,John
>>> number
'14'
>>> name
'John'

Note that they are both strings

Alex
  • 5,950
  • 3
  • 18
  • 36