What should I replace the '[0-9]' with so that it gives an answer 1 if any numeric character is present in the string?
word='[0-9]'
x='This is 2 random strings'
if word in x:
print(1)
else:
print(0)
CodePudding user response:
You could give a quick read on the re
documentation
https://docs.python.org/3/library/re.html
You could use re.search()
:
import re
if re.search(word, x):
print("number found")
CodePudding user response:
There are also other ways to achieve the same result. The below one use an any() for loop:
x = 'This is 2 random strings'
def containDigit(string):
if any(character.isdigit() for character in string):
return 1
else: return 0
print(containDigit(x))