Check if a character is inside a string via a variable in Python? -
this question has answer here:
- check if string contains number 7 answers
i newbie in python. making program take input user , check if number inside in string. checking taking in variable. not correct check via variable?
user_string=input("enter word:") print (user_string) index in (0,9): number=str(index) #typecasting int string if number in user_string: #check if number exist in string print ("yes")
output:
enter word:helo2 helo2
you can use string method isdigit()
on each character in generator expression within any
. short-circuit upon finding first numeric character (if 1 present)
>>> user_string = 'helo2' >>> any(i.isdigit() in user_string) true >>> user_string = 'hello' >>> any(i.isdigit() in user_string) false
Comments
Post a Comment