0

I have seen the below link for my answer but still didn't get an expected answer.

If I provide Name and Number in one line, Python should take the first value as a string and second as an integer.

quamrana
  • 37,849
  • 12
  • 53
  • 71
  • 2
    what have you tried so far ? any example and expected output ? – Linh Nguyen Mar 06 '20 at 10:34
  • Does this answer your question? [How to input 2 integers in one line in Python?](https://stackoverflow.com/questions/23253863/how-to-input-2-integers-in-one-line-in-python) – Masklinn Mar 06 '20 at 10:35
  • @Masklinn I did't get proper solution from this one [How to input 2 integers in one line in Python?]: https://stackoverflow.com/questions/23253863/how-to-input-2-integers-in-one-line-in-python – Mahendrasing J Pardeshi Mar 06 '20 at 10:49

4 Answers4

6
a, b = [int(x) if x.isnumeric() else x for x in input().split()]

It will convert to int any part of input if possible.

Piotr Wasilewicz
  • 1,751
  • 2
  • 15
  • 26
0
s, num = input("Give str and int :\n").split()
num = int(num)
print(s, type(s))
print(num, type(num))

Output :

Give str and int :
hello 23
hello <class 'str'>
23 <class 'int'>
Phoenixo
  • 2,071
  • 1
  • 6
  • 13
0

If you're certain to have a string and a number you can use list comprehension to get the values of both.

x = "Hello 345"

str_value = "".join([s for s in x if s.isalpha()])  # list of alpha characters and join into one string
num_value = int("".join([n for n in x if n.isdigit()]))  # list of numbers converted into an int

print(str_value)
>> Hello

print(num_value)
>> 345
Meshi
  • 470
  • 4
  • 16
0

You can get the string and int by using regular expressions as follows:

import re

input_text = "string489234"

re_match = re.match("([^0-9]*) ?([0-9]*)", input_text)

if re_match:
    input_str = re_match.group(1)
    input_int = re_match.group(2)

see: https://docs.python.org/3/library/re.html