How to Check if a String Ends with an Alphanumeric Character in Python

In this tutorial, we’ll learn how to program “How to Check if a String Ends with an Alphanumeric Character in Python.” The objective is to safely check whether a string ends with an alphanumeric character. This tutorial will guide you through the proper method for determining if a string ends with an alphanumeric character. So, let’s get started!

This topic is straightforward to understand. Just follow the instructions I provide, and you’ll complete it with ease. The program I’ll demonstrate will illustrate the correct way to check if a string ends with an alphanumeric character. So, let’s dive into the coding process!

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. import re
  2.  
  3.  
  4. regex = '[a-zA-Z0-9]$'
  5.        
  6.  
  7. def check_string(string):
  8.  
  9.        
  10.         if(re.search(regex, string)):
  11.                 print("String Accepted!")
  12.                
  13.         else:
  14.                 print("String not Accepted!")
  15.        
  16.  
  17.  
  18. if __name__ == '__main__' :
  19.        
  20.         while True:
  21.                
  22.                 print("\n============ Check if a String Ends with an Alphanumeric Character ============\n\n")
  23.                 my_string = input("Enter a string: ")
  24.        
  25.                 check_string(my_string)
  26.  
  27.                 opt = input("\nDo you want to try again?(yes/no): ")
  28.  
  29.                 if opt.lower() == 'yes':
  30.                         ret=False
  31.                 elif opt.lower() == 'no':
  32.                         ret=True
  33.                         print("Exiting program....")
  34.                 else:
  35.                         print("Please enter yes/no:")
  36.                         break
  37.  
  38.                 if ret == False:
  39.                         continue

This script repeatedly prompts the user for input and checks whether the entered string ends with a letter or number, printing 'String Accepted!' if it does and 'String not Accepted!' otherwise.

Output:

There you have it we successfully created How to Check if a String Ends with an Alphanumeric Character in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials

Add new comment