[Tutor] Python strings example

Ankit Gupta ankitgupta38017 at gmail.com
Mon Apr 26 12:20:19 EDT 2021


In 2nd example you return will look something like this "return False and
True" which will result in False as AND operation will only return TRUE if
both value is TRUE.
as I can understand you want to return TRUE if sentence contain at least
one character or at least one digit or both. Therefore I will suggest to
use OR instead of AND operation.

Ankit Gupta.

On Mon, 26 Apr, 2021, 9:19 pm Manprit Singh, <manpritsinghece at gmail.com>
wrote:

> Dear sir,
>
> consider a problem where I have to write a function that can return True if
> the string passed to the function contains at least one digit character and
> one alphabet character, else the function must return False. The way i am
> solving it is given below :
> def str_chk(word_tochk):
>     dig = False
>     alpha = False
>     for ele in word_tochk:
>         dig = dig or ele.isdigit()
>         alpha = alpha or ele.isalpha()
>     return dig and alpha
>
>
> st = "Abhay2"
> ans = str_chk(st)
> print(ans) returns True, which is correct as the string contains an
> alphabet as well as a digit character.
>
> st = "Abhay"
> ans = str_chk(st)
> print(ans) returns False . which is also correct as there is no digit
> character in the string now
>
> Any suggestions, if the solution can be improved .
>
> Regards
> Manprit Singh
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list