Python | 4 Ways to Check if Word is in English or Not

Python can be used for dealing with Big Data or Natural Language Processing, companies like Google which analyse text and interpret webpages to rank them use these techniques for better understanding language or to distinguish between text from different languages. On small scale, like if you have a text file of let’s say 10 MB or so then using Python can be really helpful for checking whether words in text file are in English Language or not. There exists four ways to do this using Python which are –

  • Using isalpha method for checking Word is in English or not
  • Using Regular Expression for checking Word is in English or not
  • Using Operators for checking Word is in English or not
  • Using lower and upper methods for checking Word is in English or not

Let’s discuss each one of this method one-by-one.

Using isalpha method for checking Word is in English or not

In Python, string objects have a method called isalpha() which can be defined as => Returns True if all characters in string are alphabetic and Returns False if even single character in string is not alphabetic.
Below is a Python Code example showing How to use isalpha method for checking whether word is in English or not?

word = "ComputerScienceHub"

if word.isalpha():
	print(word, " => is in English Language")
else:
	print(word, " => is not in English Language")

Using Regular Expression for checking Word is in English or not

Python’s re Module can be used for comparing an Alphabetic Regular Expression with Word. Let’s make a Regular Expression which covers up all of English Alphabets => r'[a-z]’
So comparing Word with this regular expression can be used a Verification test for checking whether word is in English language or not.
Below is a Python Code example showing How to use Regular Expression for checking whether word is in English or not?

import re
word="ComputerScienceHub"
reg = re.compile(r'[a-z]')

if reg.match(word):
    print(word, " => is in English Language")
else:
    print(word, " => is not in English Language")

If your not aware of Python’s re Module then check out this article => Regular Expressions in Python re Module

Using Operators for checking Word is in English or not

Python’s Comparison Operator can also be used for checking whether Word is in English Language. Below is a Python Code Example Showing this.

word = "ComputerScienceHub"

if('a' <= word[0] <= "z" or 'A' <= word[0] <='Z'):
    print("It is in English Language")
else:
    print("It is not in English Language")

Using lower and upper methods for checking Word is in English or not

As English alphabetic characters can either be lowercase(like => a b c) or uppercase(like => A B C). But Python treats these characters differently, so in eyes of Python lowercase a b c is different than uppercase A B C.
Below is a Python Code example showing How to use lower and upper methods for checking whether word in English or not?

word = "ComputerScienceHub"
if word.upper() != word.lower():
    print("It is in English Language")
else:
    print("It is not in English Language")

Final Thoughts

Each of these four methods are not always enough for checking wether word is in English or not, because for some other languages these four tests can be true. So better approach if in case you want to be 100% sure that word is in English Language. Then just use all of these four methods together.
Below is a Python Code Example showing Best Approach for checking whether Word is in English or not using Python?

import re
word = "ComputerScienceHub"

if (word.isalpha() and re.compile(r'[a-z]')):
	if (('a' <= word[0] <= "z" or 'A' <= word[0] <='Z') and (word.upper() != word.lower())):
		print(word, " => is in English Language")
else:
	print(word, " => is not in English Language")

That’s it, I hope that you understand this article very well. If you’ve some question then please comment down below and I’ll reply as soon as possible.
Happy Coding 🥳

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts