-1

i am trying to solve a problem where i want to classify string and int separately. My question is when user input anything then my program find all words(str) and int separately.

Like user input "wert1234" then it should print "string is wert" and int are 1234"

I tried with many ways but i am facing problem i tried with :

in=input()
for i in in:
    if type(i)==type(str):
       print(i)

but its not working i also tried with regex :

import re
df=input()
regex = r"([a-zA-Z]+) \d+"
matches = re.findall(regex, df)
for match in matches:
    print(match)

stri = r"([0-9]+) \d+"
search=re.findall(ret,df)
for j in search:
    print(j)

so i have two question:

first please tell how i can classify string and int differently if input is mix ?

second question is how search only int with regex? like if user input "wer 123" so regex search only 123 and print.

TOpchegf
  • 61
  • 2
  • 6

1 Answers1

0

Extract integers and words like this:

import re

my_string = 'wert1234word0test12'

integers = re.findall('\d+', my_string)
words = re.findall('[a-zA-Z]+', my_string)

Output:

>>> integers
['1234', '0', '12']
>>> words
['wert', 'word', 'test']

You may want to convert items in integers list to be of type int, you can achieve that by using list comprehension:

>>> [int(i) for i in integers]
[1234, 0, 12]
ettanany
  • 19,038
  • 9
  • 47
  • 63