Python

Isalnum python

Like many other languages, Python has come up with many different data types for use. As python doesn’t declare variables with their data types, it’s quite easy to use mixed values. One of those data types is “alphanumeric”, which is the mix of numbers and alphabets. Values other than alphabets and numbers are not considered alphanumeric. Python has come up with the isalnum() function to check out the alphanumeric value in a variable and returns the boolean result. To check out the isalnum() function of python in the Ubuntu 20.04 Linux system, we must have python’s latest version already installed and configured. If you haven’t installed it, try installing it using Ubuntu’s shell terminal application.

On your desktop of Ubuntu 20.04, try Ctrl+Alt+T to open the terminal quickly. After the launch of the shell, we have to make use of the “apt” instruction with the “install” keyword and the name of a package to be installed, i.e. “python3”. This will start the installation of python3 by requesting you to enter your account password. You have to enter your sudo account password and tap Enter to continue this process. Within no more than 10 minutes, it will be installed on your system.

After the python is successfully installed and configured on our Ubuntu 20.04, we have to create a python file in which we can create our code. To generate a python file, you need to utilize the “touch” query on the query area of a shell with the file’s name containing the “.py” extension. Your newly generated file can be opened from the system’s file explorer. Most of the time, it resides in the home folder. We have been using the instruction method to directly open the new empty python file in the built-in Gnu nano editor. Use the word “nano” with a file name, and your file will be opened in the shell’s nano editor. You can also open your new file in the other built-in editors like vim and text by simply using the shell instructions or double-tapping on the file itself. Let’s get started with the example now.

Example 01:

We will start this guide with the basic python code example to use the isalnum() function. We need to check out a string value with numbers and alphabets first, as the isalnum() function is used to check whether the string is alphanumeric or not. So, we will be doing that. But before that, we need to add the python support in our python new file. To add it, make use of the “#!/usr/bin/support” line at the very start of this file. After this, our file is ready to create python code and execute it.

As the isalnum() function is a built-in function, we don’t have to import any library for it. Now, we have to declare and initialize a variable with some value. Thus, we have declared a variable “x” and initialized it with a numerical value “1234” in double quotes using the “=” assignment operator. In python, you don’t have to add “;” after each line or instruction in the code. On the other hand, you need to pay much attention to the code indentation. This is because the python language is quite sensitive about the indentation, and you have to put the indentation wherever required.

After declaring the “x” variable with some value, you need to call the isalnum() function with this variable “x”. The reference will be used here to do that. To call the isalnum() function by the “x” variable, you have to put “.” In between. You can see it’s more like an object call to a specific function, as we mostly do in object-oriented languages. Calling function by variable will return a boolean value, i.e. true or false. If the variable contains some alphanumeric value, it will return “True” otherwise “false”. To store the boolean value returned by the isalnum() function, we have been assigning a value to a new variable, “result”. At last, we have been using python’s print statement taking the “result” variable in its argument to display the boolean result got by the isalnum() function.

We don’t need any compiler for running the python code as python is an interpreter language. We just have to use the python3 support in the shell terminal and the name of a python file as we did beneath in the image screenshot. The code got executed, and we have got the boolean value “True” as a result. This shows that the string “x” contains an alphanumeric value.

Let’s update our code by replacing the numerical value “1234” with the alphabetic value “ABC” in the variable “x” to check what the isalnum() function returns, as shown below.

After saving this python code, we have executed the file and got “true” in return, i.e. “x” is alphanumeric.

Let’s use the mix of numbers and alphabets in the variable “x” value. So, we have updated the variable “x” with the “New12” value as shown. The rest of the code remains untouched.

The execution of this python code also returns “true”, showing that the value is alphanumeric.

Example 02:

Let’s take a look at another example of using the isalnum() function in python code. So, we have added the same python-support at the start of a file and initialized a variable “x” with a mixed value. Variable “x” value contains alphabets, numbers, numerical operators, and special characters as well. The variable “r” is getting the returned result from the “isalnum()” function after calling it by variable “x”. The print statement uses using “r” variable to show whether the “X” variable is alphanumeric or not.

After running this code with the python3 support instruction, we have got “False” in return. This is because we have used numerical operators and special characters instead of using only alphabets and numbers.

Example 03:

Let’s take a look at the last but not the least example of using the isalnum() function in python code. This time, we will be using it to test three different variables x, y, and z. The variable x contains a space as a value, variable y contains alphabets and space, while the variable z contains alphabets and numbers. Three variables, r1, r2, and r3, are used to get the result generated by the isalnum() function called by the x, y, and z variables one after another. Respectively, three print statements are here to display the result of each variable, r1, r2, and r3, separately. We expect from first 2 print statements to display “False” and the last print statement to display “True”.

After saving our python code file with “python3” support, we have got the below-shown result in return. It is the same as we expected, i.e. shows “False” for the first 2 variables “, x” and “y”, and for the last variable “, z”, it shows True.

Conclusion:

This is all about using the isalnum() function in python while using the Ubuntu 20.04 Linux distribution. We have seen very simple examples of python using the isalnum() function to show whether the given variable value is alphanumeric or not. This article is for those python users who are unaware of the isalnum() function.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.