This is what I have so far:
alphabet = "a" or "b" or "c" or "d" or "e" or "f" or \
"g" or "h" or "i" or "j" or "k" or "l" or \
"m" or "n" or "o" or "p" or "q" or "r" or \
"s" or "t" or "u" or "v" or "w" or "x" or \
"y" or "z"
letter_word_3 = any(alphabet + alphabet + alphabet)
print("Testing: ice")
if "ice" == letter_word_3:
print("Worked!")
else:
print("Didn't work")
print(letter_word_3) # just to see
I want to be able to eventually scan a document and have it pick out 3 letter words but I can't get this portion to work. I am new to coding in general and python is the first language I've learned so I am probably making a big stupid mistake.
To check if a string contains any letter, use the test() method with the following regular expression /[a-zA-Z]/ . The test method will return true if the string contains at least one letter and false otherwise. Copied!
Python String isalpha() The isalpha() method returns True if all characters in the string are alphabets. If not, it returns False.
To check if a character is a letter, call the test() method on the following regular expression - /^[a-zA-Z]+$/ . If the character is a letter, the test method will return true , otherwise false will be returned.
isalnum() is a built-in Python function that checks whether all characters in a string are alphanumeric. In other words, isalnum() checks whether a string contains only letters or numbers or both. If all characters are alphanumeric, isalnum() returns the value True ; otherwise, the method returns the value False .
You've got some good ideas, but that kind of composition of functions is really reserved for functional languages (i.e. syntax like this would work well in Haskell!)
In Python, "a" or "b" or ...
evaluates to just one value, it's not a function like you're trying to use it. All values have a "truthiness" to them. All strings are "truthy" if they're not empty (e.g. bool("a") == True
, but bool("") == False
). or
doesn't change anything here, since the first value is "truthy", so alphabet
evaluates to True
(more specifically to "a"
.
letter_word_3
then tries to do any("a" + "a" + "a")
, which is always True
(since "a"
is truthy)
What you SHOULD do instead is to length-check each word, then check each letter to make sure it's in "abcdefghijklmnopqrtuvwxyz"
. Wait a second, did you notice the error I just introduced? Read that string again. I forgot an "s"
, and so might you! Luckily Python's stdlib has this string somewhere handy for you.
from string import ascii_lowercase # a-z lowercase.
def is_three_letter_word(word):
if len(word) == 3:
if all(ch in ascii_lowercase for ch in word):
return True
return False
# or more concisely:
# def is_three_letter_word(word):
# return len(word) == 3 and all(ch in ascii_lowercase for ch in word)
There are a couple things wrong. First off alphabet
is always being evaluated to "a"
.
The or
in the declaration just means "if the previous thing is false, use this instead." Since "a"
is truthy, it stops there. The rest of the letters aren't even looked at by Python.
Next is the any
. any
just checks if something in an iterable
is true. alphabet + alphabet + alphabet
is being evaluated as "aaa"
, so letter_word_3
always returns True
.
When you check if "ice" == letter_word_3
' it's being evaluated as "ice" == True
.
To check if an arbitrary word is three letters, the easiest way is using the following:
import re
def is_three_letters(word):
return bool(re.match(r"[a-zA-Z]{3}$", word))
You can then use
is_three_letters("ice") # True
is_three_letters("ICE") # True
is_three_letters("four") # False
is_three_letters("to") # False
is_three_letters("111") # False (numbers not allowed)
To also allow numbers, use
import re
def is_three_letters(word):
return bool(re.match(r"[a-zA-Z\d]{3}$", word))
That'll allow stuff like "h2o" to also be considered a three letter word.
EDIT:
import re
def is_three_letters(word):
return bool(re.match(r"[a-z]{3}$", word))
The above code will allow only lowercase letter (no numbers or capitals).
import re
def is_three_letters(word):
return bool(re.match(r"[a-z\d]{3}$", word))
That'll allow only lowercase letters and numbers (no capitals).
EDIT:
To check for n amount of letters, simply change the "{3}" to whatever length you want in the strings in the code above. e.g.
import re
def is_eight_letters(word):
return bool(re.match(r"[a-zA-Z\d]{8}$", word))
The above will look for eight-long words that allow capitals, lowercase, and numbers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With