Technology3 min read

Python String methods - isalpha(), isalnum() and isascii()

Abhishek Ahlawat

By Abhishek Ahlawat — Recently

Founder of StudyTonight and a MERN fullstack developer. Writes about JavaScript, web development, Linux, and tech.

#Python#Python Strings#Programming

Summarize this article using AI

We are well aware of the string data type in Python. It is the most used datatype when it comes to python programming. A string can be defined as a sequence of characters, and that's the most basic explanation of string that you can provide. There are innumerable string functions which come in handy for carrying out operations on strings.

In today's post, we will understand three methods associated with strings.

  1. isalpha

  2. isalnum

  3. isascii

Let us see the syntax and use of the above 3 methods with code examples.


1. isalpha Python method:

In Python, the isalpha method is a built-in function and it checks for the presence of alphabets-only in a string. If all the characters in the string are alphabets, it returns True. If the string contains other characters apart from alphabets for example a number or any special characters, it returns False. Alphabets in the English language include characters from A-Z and a-z.

Syntax of the isalpha python method

string.isalpha()

Here the string refers to the string in question, i.e the string which is being checked for containing only alphabets or not.

It doesn't take any parameters.

It returns True or False depending on the characters inside the string. There should at least be one character inside the string for the method to evaluate the string. Look at the below code example to understand the use of this function:

my_string = 'Studytonight'
print(my_string.isalpha())

my_string = 'Study tonight'
print(my_string.isalpha())

my_string = 'Studytonight@123'
print(my_string.isalpha())

my_string = '  '
print(my_string.isalpha())

my_string = ''
print(my_string.isalpha())

Output:

True
False
False
False
False

Note: Whitespaces, numbers and special characters are not considered as alphabets.


2. isalnum method

The isalnum method can be called as an extension of the isalpha python method. In addition to checking for the presence of alphabets in the string, the isalnum method checks whether the method contains numeric characters too. The method checks for the presence of alphanumeric characters(alphabets + numbers only).

Syntax of the isalnum method

string.isalnum()

Just like the isalpha python method, the string here refers to the string which is being checked for containing alphanumeric characters or not.

This method also doesn't take any parameters.

It returns True or False depending on the characters present the string.

In the below code, it can be observed that the isalnum method only approves for alphabets and numbers in a string. No special characters and white spaces are entertained.

my_string = 'Studytonight'
print(my_string.isalnum())

my_string = 'Study tonight'
print(my_string.isalnum())

my_string = 'Studytonight@123'
print(my_string.isalnum())

my_string = 'Studytonight123'
print(my_string.isalnum())

Output:

True
False
False
True


3. isascii method

As the name of the method suggests, this method checks whether the string contains ASCII characters, i.e checks whether the string is printable or not.

Syntax of isascii method

This method has been newly introduced in Python version 3.7 and following is the syntax for it,

string.isascii()

This method doesn't take any parameters and the string refers to the input string which is checked for the presence of ASCII characters.

It returns True if the string is empty or if all the characters in the string are ASCII characters. Otherwise, it returns False. ASCII characters refer to characters that have a printable representation. Their code points lie between the range U+0000 to U+007F.

For example, ° is changed to \\xb0. Below is the code demonstrating how the degree symbol outputs as a string.

print(ascii('°'))
print(type(ascii('°')))

Output:

"'\\xb0'"
str

Time for an example:

my_string = '  '
print(my_string.isascii())

my_string = 'Studytonight'
print(my_string.isascii())

my_string = 'Study tonight'
print(my_string.isascii())

my_string = 'Studytonight@123'
print(my_string.isascii())

my_string = '°'
print(my_string.isascii())

my_string = 'ö'
print(my_string.isascii())

Output:

True
True
True
True
False
False


Conclusion

In today's post, we understood how to check a string for the presence of alphabets, alphanumeric characters, and ASCII characters. Don't forget to meddle with the code to see what works and what doesn't.

You may also like:

Spotted something outdated or wrong?

Liked this? See more StudyTonight posts in Google results.

Add StudyTonight as a preferred source
Next questLIVE OPSStep 2 / 3

Practice real incidents in the browser

System Design Ops — put out fires, ship fixes, and stack XP. Reading done. Time to play.

Play System Design
  • +XP

    Every sim

  • Incidents

    Live ops

  • Ranks

    Climb up

Read next