How to check whether a string contains all the letters of the alphabet or not in python?

How to check whether a string contains all the letters of the alphabet or not in python?

Python string

Vipul kunwar's photo
Vipul kunwar
ยทNov 1, 2021ยท

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Recently, I and my friends played a game.

Do you know what's the game is about?

The game is to create a sentence that contains all the letters of the alphabet.

Although, I'd lost the game. But it gives me a quick flash.

The flash that, can we create a sentence in python and check whether it contains all the alphabets or not?

So, I've studied and found ๐Ÿ” out.

I've found so, first of all, you can do it.

But for that, you need to import the string module.

  1. Import the string module

2๏ธ.Create a variable that stores the ascii_lowercase value.

Note: The ascii_lowercase value used to create alphabets in lowercase. It's a constant value.

3.Use set() method to convert string into individual items to compare.

4๏ธ.Likewise, create another variable that stores the string that you want to check.

5.All of the above, compare the ASCII value to the second variable of the sentence.

*Return value:

If the second variable that is sentence contains all the alphabets of ASCII value, it'll return True. Otherwise, False.*

Let's see this in code!

## Code

image.png

In the above code, you'd seen first I've imported the string using import statement.

Second, I've created the variable alphabet.

In this variable, I've done the ascii_lowercase.

For example,

alphabet = set(string.ascii_lowercase)

If you observe then I've used the set() method.

So, why I used it?

I've used it. because it'll take a string and make a collection of individual items.

The reason behind is that if you only compare the string with another sentence of string.

Then it'll compare the ASCII values%20function.&text=To%20get%20the%20character%20encoded,use%20the%20chr()%20function.&text=To%20know%20if%20all%20the,use%20the%20isalnum()%20function.) of the alphabets.

For analogy,

print("t" >= "rs") ---> return true.

Visually, that doesn't mean the alphabet "t" is presented in second string "rs".

So, you can see that I've to convert it into individual alphabets.

Thus, I'm going to use set().

Take a different approach,

For example,

print(set("t") >= set("rs"))

True

Here's the catch, you've to convert string into set. Thus, you can find individual alphabets are present into string or not.

After, this analogy you've gained the insight about set() method.

Second, I've created a variable which stores a sentence of string.

input_string = 'The quick brown fox jumps over the lazy dog'

At last, I've compared both string. And I've gained a output.

So, In short my code is --->

**#import the string module to use all string manipulation task import string alphabet = set(string.ascii_lowercase)

input_string = 'The quick brown fox jumps over the lazy dog' print(set(input_string.lower()) >= alphabet)**

%Run hey.py True

So, you'd seen the output ---> True. That means ALL alphabets are present in string.

But suppose, I change last word to 'cat'.

Then program will be

**#import the string module to use all string manipulation task import string alphabet = set(string.ascii_lowercase)

input_string = 'The quick brown fox jumps over the lazy cat' print(set(input_string.lower()) >= alphabet)**

%Run hey.py False

Here, you can see that output has come False. because alphabet 'd' isn't present.

## Conclusion

Finally, you've come to a point where you've seen the how to check if sentence contains all letters or not.

Meanwhile, you've seen how to use some part of the string module.

Also, you've seen that WE have to use set() method to check individual alphabets in sentences.

Likewise, I've compared the string.

So, this awesome game play with your friend.

twitter.com11_Vipul kunwar


Did you find this article valuable?

Support Vipul kunwar by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
ย 
Share this