-2

I'm newbie to python. I'm trying to find matching digits in a string from a given number. I have tried the following code:

st=input("Enter a string")
no=int(input("Enter a number"))
for i in range(len(st)):
while no>0:
rem=no%10
if rem == st[i]:
print(rem)  
  • 4
    Indentation is very important in python. Yours is messed up - maybe it was a cut and paste problem. Please fix it to make it easier for the community to read your code. And here's a tip for using this site: tell us what happened when you tried to run your code. Don't make us guess. That just makes it less likely that someone will spend the time to help you. – Paul Cornelius Apr 22 '20 at 10:19
  • 1
    You're comparing a string against a number, either use `str(rem) == st[i]` or `rem == int(st[i])` – Nick stands with Ukraine Apr 22 '20 at 10:55

1 Answers1

0

Here's a code snippet for you:

# Number input
mynumber = input("Enter a number: ")

# Check if input is an integer
try:
    int(mynumber)
except ValueError:
    print("Sorry, not a number")

# String input
mystring = input("Enter a string: ")

# Look for matching numbers in the string and display them
match = ''

for n in mynumber:
    for s in mystring:
        if n == s and not n in match:
            match += n

print('Matching:', match if match else 'None')
Lore
  • 34
  • 7
  • Thanks for your response. I want number as integer datatype instead of string. Please may you help me for this? – Caroline Apr 25 '20 at 09:39
  • You can use `int(match)` to cast the string as integer. See [python doc](https://docs.python.org/3.4/library/functions.html?highlight=int#int). – Lore Apr 25 '20 at 13:49