How to make my own isalpha() function?


#1



Hi,
I am begginer in programming and going through all excercises.
I was curious how isalpha() function code looking like. I tried to do my own exrcise to try to make it my own method which would work similar to isalpha().

You can see in a code below I made all the characters from input compare to the list and it returns True or False for each character.

How a code for isalpha() would look like, so I could understand it. Please advise.


type_in = input()
numbers = ["0","1","2","3","4","5","6","7","8","9","0"]

for i in type_in:
    if i in numbers:
        print("True")
    else:
        print("False")


#2

isalpha() function ..

1.It does not take any parameter(s).
2.It returns True (Boolean values) if a string only consist of alphabets.
3.Otherwise it returns False. (means It indicates that string must have special character,number character or spaces inside it )

example...

"123Imstring".isalpha()  # return False as it consists of number characters.
"@imstring".isalpha() # return False as it consists of special character ( @).
"im string".isalpha() # return False as it consists of space character.
"imString".isalpha() # return True as it consists of only Alphabets (Upper or Lower both cases are accepted).

#3

Thank you for explaining how it properly works.

What I was curious is .isalpha() is a function itself and it was written to behave this way.

I just wanted to know how a code for this method is written to behave this way as you could write your own method which works exactly the same as .isalpha.
Hope I didnt make it complicated.

Thanks for help


#4

It can be...
the rough and basic solution I came with is this.. ( I have tested for few cases,It passed, though I highly reason that it needs to be tested more but anyways for Ideas)

@addictions

def checkS(Ex_string):
    for i in Ex_string:
        if i in "0123456789":
            return "it consists of number(s)."
        elif i in "~!@#$%^&*()_+-=|`.,":
            return "it consists of special character(s)."
        elif i in " ":
            return "it consists of spaces"
    else:
        return "It consists of Alpahabets."

few tests that I did..


#5

This is great.
It looks like I was not far away from proper solution.
It really helps for a beginner to understand things in more detail.


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.