Glen Veigas
Glen Veigas

Reputation: 75

How to find if a number is a subset of a given integer in Python

I am writing a program with python to find if a number is present as a set in another number

for example: if the number, a = '123456789010234 and I have to find if '4567' is present in a or not.

Upvotes: 0

Views: 620

Answers (5)

Yeganeh Salami
Yeganeh Salami

Reputation: 595

you can use

if '4567' in a:
   ...

Upvotes: 2

Sayandip Dutta
Sayandip Dutta

Reputation: 15872

If you are receiving both the numbers as strings:

a = '123456789010234'
b = '4567'
print(True if b in a else False)

If you are receiving as ints, convert to string and check with in operator.

If you need to check whether the strings are integer at all, or need to handle negative integers, you need to use try-except.

Upvotes: 2

oppressionslayer
oppressionslayer

Reputation: 7214

Here you go:

if '123456789010234'.find('4567') > 0: 
     print ("Found!") 
  else: 
     print("Not Found!") 

#Found!

if '123456789010234'.find('4d567') > 0: 
     print ("Found!") 
else: 
      print("Not Found!") 

#Not Found!


Upvotes: 2

marcos
marcos

Reputation: 4510

If a is a string you can just do this:

a = '123456789010234'
value = '4567'

if value in a:
    print(f'Value {value} present in {a}')
else:
    print(f'Value {value} not present in {a}')

Upvotes: 2

A.J. Uppal
A.J. Uppal

Reputation: 19264

Try using in, if both are integers:

str(num) in str(a)

>>> a = 123456789010234
>>> num = 4567
>>> str(num) in str(a)
True

Upvotes: 3

Related Questions