Check if a character is inside a string via a variable in Python? -


this question has answer here:

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

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -