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!”
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?
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!!
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.
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'
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.
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”