
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Program to accept string ending with alphanumeric character
When it is required to check if a string ends with an alphanumeric character or not, the regular expression is used. A method is defined that checks to see an alphanumeric characters, and returns the string as output.
Example
Below is a demonstration of the same
import re regex_expression = '[a-zA-z0-9]$' def check_string(my_string): if(re.search(regex_expression, my_string)): print("The string ends with alphanumeric character") else: print("The string doesnot end with alphanumeric character") my_string_1 = "Python@" print("The string is :") print(my_string_1) check_string(my_string_1) my_string_2 = "Python1245" print("\nThe string is :") print(my_string_2) check_string(my_string_2)
Output
The string is : Python@ The string doesn’t end with alphanumeric character The string is : Python1245 The string ends with alphanumeric character
Explanation
The required packages are imported.
A regular expression string is defined.
A method named ‘check_string’ is defined, and it takes the string as a parameter.
The ‘search’ method is called and checked to see if a string ends with a specific character.
Outside the method, the string is defined and displayed on the console.
The method is called by passing this string.
The output is displayed on the console.
- Related Articles
- Python Program to accept string starting with vowel
- Java regex program to verify whether a String contains at least one alphanumeric character.
- Program to find whether a string is alphanumeric.
- MySQL query to select column values ending with certain character/number?
- PHP program to remove non-alphanumeric characters from string
- Program to find sum of digits that are inside one alphanumeric string in Python
- Alphanumeric Abbreviations of a String in C Program?
- How to check if a string is alphanumeric in Python?
- Python program to find Most Frequent Character in a String
- Python program to find Least Frequent Character in a String
- Python program to find occurrence to each character in given string
- Python program to check if a string contains any unique character
- Python program to change character of a string using given index
- Removing nth character from a string in Python program
- Python program for removing nth character from a string

Advertisements