Python - Check Numeric Suffix in String


Generally, a suffix refers to an affix or a group of letters that is added to the end of a word to modify its meaning or to form a new word. Suffixes are used in linguistics and grammar to create different word forms, such as plurals, verb tenses, comparatives, and more.

Suffixes can change the meaning or function of a word in various ways. For example, the suffix "-s" added to the word "cat" forms the plural "cats," indicating multiple cats. Similarly, the suffix "-ed" added to the verb "walk" creates the past tense "walked," indicating an action that occurred in the past.

In the view of strings, a suffix refers to a sequence of characters that appears at the end of a string. It is the portion of the string that comes after all other characters.

For example, in the string "example123", the suffix is "123". Similarly, in the string "hello_world", the suffix is "_world". The suffix can be of any length, from a single character to the entire remaining portion of the string.

In this article we are going to learn all the possible approaches to check the numeric suffix in given string.

Using Regular Expressions

The re module in python supports the regular expressions for finding the defined pattern in the given string. There are multiple methods and functions available to work with the string patterns.

Example

In this example, the has_numeric_suffix() function uses the re.search() function to search for a pattern at the end of the string. The pattern r"\d+$" matches one or more digits (\d+) at the end of the string ($). If a match is found, the function returns True otherwise, it returns False.

import re
def has_numeric_suffix(string):
   pattern = r"\d+$"  # Matches one or more digits at the end of the string
   match = re.search(pattern, string)
   return match is not None
print(has_numeric_suffix("python123"))  
print(has_numeric_suffix("python"))

Output

True
False

Using String Manipulation

Python provides several built-in string methods that can be used to manipulate and check strings. We can use these methods to extract the last characters of the string and check if they are numeric.

Example

In this example, the has_numeric_suffix() function extracts the last character of the string using string slicing (string[-1:]) and then, the isdigit() method checks if the extracted character is a digit. If it is, the function returns True otherwise it returns False.

def has_numeric_suffix(string):
   last_chars = string[-1:]  # Extract the last character
   return last_chars.isdigit()
print(has_numeric_suffix("python 23")) 
print(has_numeric_suffix("tutorialspoint123"))

Output

True
True

Using the str.endswith() Method

The str.endswith() method in Python checks if a string ends with a specified suffix. We can use this method to check if a string ends with a numeric suffix by passing a numeric suffix as the argument.

Example

In this example, the has_numeric_suffix() function uses a generator expression (tuple(str(i) for i in range(10))) to create a tuple of strings representing the digits from 0 to 9. The endswith() method is then called on the string, passing the tuple as the argument. If the string ends with any of the numeric suffixes, the method returns True otherwise, it returns False.

def has_numeric_suffix(string):
   return string.endswith(tuple(str(i) for i in range(10)))
print(has_numeric_suffix("python123"))  
print(has_numeric_suffix("Tutorialspoint"))

Output

True
False

Updated on: 07-Aug-2023

104 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements