Existence check in python | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

Existence check in python

hey guys how can I define a function which checks if a digit exists in a number? ( I'm talking about integers. )

25th Apr 2021, 6:34 PM
Hossein Karbalaei
6 Réponses
+ 3
# is this what you are searching for? def myfunc(num, digit): if str(digit) in str(num): return "Exist" return "Does not exist" print(myfunc(12345, 3))
25th Apr 2021, 8:27 PM
iTech
iTech - avatar
+ 2
The simple way is to convert the int to a string. Then search for the digit in the string.
25th Apr 2021, 7:03 PM
Jerry Hobby
Jerry Hobby - avatar
+ 1
Well yeah I got that thank y'all. I meant how can we check if a digit exists in a number. For example whether 3 exists in 76567*5261788 or not. According to what you kind people said - which is true - we have to convert them into strings.
25th Apr 2021, 8:30 PM
Hossein Karbalaei
0
Did you mean the length of the intigers
25th Apr 2021, 6:52 PM
Ananiya Jemberu
Ananiya Jemberu - avatar
0
do you mean check if a given digit exist within a number? please give us an input and an output exemple to figure out what do you exactly mean.
25th Apr 2021, 7:23 PM
iTech
iTech - avatar
0
"""I'm not very good in English, but I think that's what you mean. """ isinstance(5,int) or if int == type(5): "do something"
25th Apr 2021, 7:24 PM
Amir
Amir - avatar