Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining the alphabet to any letter string to then later use to check if a word has a certain amount of characters

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.

like image 328
Ethan McRae Avatar asked Aug 13 '17 18:08

Ethan McRae


People also ask

How do you check if a string contains any letters?

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!

How do you check if a string contains any alphabet in Python?

Python String isalpha() The isalpha() method returns True if all characters in the string are alphabets. If not, it returns False.

How do you check if a character in a string is a letter Javascript?

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.

How do you check if a string contains letters and numbers in Python?

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 .


2 Answers

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)
like image 119
Adam Smith Avatar answered Sep 28 '22 08:09

Adam Smith


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.

like image 24
ChristianFigueroa Avatar answered Sep 28 '22 09:09

ChristianFigueroa