How do I use isalpha( )?

Question

How do I use isalpha( )?

Answer

isalpha() is a method that can be used on any string - whether it be a variable holding a string, or a string by itself, or a string returned from another method. We use it in this PygLatin translator to check if the user of our program inputs anything other than alphabetical characters, A-Z a-z.
To make use of it, we simply attach it to the end of a string we want to check for that condition - being solely alphabetical. Like this:

my_string = “Not 4lphabet1cal”
if my_string.isalpha():
    print “isalpha() returned True; the string was entirely alphabetical!”
else: 
    print “something in that string was NOT alphabetical!”
5 Likes

Why does .isalpha() returns False when there is a space between two words, even though all of them consist of letters?

You said it yourself. Those are not all letters.

2 Likes

But they are still all letters without the space between them? I am doing the Pig Latin project here currently using Python. Wanted to translate ‘Hello World’ into pig latin. Is there a way .isalpha() will ignore the space?

If str.isalpha ignored non-letters it would be useless. Its purpose is to tell those apart.

If you know how to do something once then you also know how to do it twice by repeating for the second occurrence

You could also remove the condition entirely. You’ll find the rules operate on one word at a time, so it doesn’t make sense to translate two words as if it was one, does it?

3 Likes

Is there a numerical equivalent to .isalpha(): ?

5 Likes

Why does returns “empty” even though the string is not empty and doesn’t have any numbers?

empty here implies that the data is useless. There is nothing to translate, or that can be translated.

Did you enter a space at the beginning of your input?

55%20AM

If so, then original would contain a space, which is not an alphabetic character.

If you want a space between the : character and the user’s input, include the space in the raw_input prompt.

5 Likes

I put the code that was need to pass the exercise, but even though codecademy is still forcing me to use the solution. Wondering what is wrong, or what I may be doing wrong. Please help!!

2 Likes

No idea what you’re doing.

I’ve noticed this happening a lot in the Python 2 course during the Pig Latin Translator part. I know my code is correct due to it printing the original, but I keep being told to get a solution. I’ve started copying my original code to compare to the solution (and see they are always identical). This is likely a bug. I reported it. You should report it too if you are getting the desired print original but still not able to move on.

1 Like


It lookslike a Chinense character is seen as alphabetic character

Me as well. I have reported it.

1 Like

You put a space before beautiful which is why it returned as “empty”

Hello,

Someone know why I cant run with this program

print 'Welcome to the Pig Latin Translator!'

# Start coding here!
raw_input('tell me a word')
original=raw_input()

if len(original)>0:
  print original
  else :
  print 'empty'
if original.isalpha():
  print 'letters'
  else:
  print 'notalpha'

I tryied it after, but gives me an error

There are two calls there to raw_input() when it should be only one…

original = raw_input("Tell me a word: ")
2 Likes

Me too! I was forced to check the answer!

Yes. The .isdigit() method returns True if every character in the given string is a digit.
This, of course, would not make sense as a method for numerical values.
You could also argue that it isn’t a “numerical equivalent” because "4.2".isdigit() returns False, but if you really wanted to know if you could convert it to a number, there are multiple valid ways of checking.

2 Likes

Hi mtf,

I don’t see any option for me to post a question/doubt on the forum. Hence, posting it here.

When I use original.lower(): condition in the following code, it doesn’t work. It prints the word even when you enter an uppercase word. Could you please clarify?

original = raw_input("Enter a word: ")
if original.lower():
print original
else:
print “empty”