Change language

Python | Check numeric suffix in string

| |

Method # 1: Using Regular Expressions
This problem can be solved by using regular expressions. The search and group function can perform the task of finding the suffix string and printing the number if necessary.

# Python3 code to demonstrate how it works
# Check the numeric suffix in the string
# Using regular expressions

import re

  
# initialization string

test_str = "Geeks4321"

 
# print original line

print ( "The original string is:" +   str (test_str))

 
# Using regular expressions
# Check the numeric suffix in the string

res = re.search (r ’ d + $ ’ , test_str)

  
# print result

print ( " Does string contain suffix number? : " + str ( bool (res)))

Output:

 The original string is: Geeks4321 Does string contain suffix number?: True 

Method # 2: Using isdigit ()
The isdigit function can be used to accomplish this specific task, using the fact that if the number at the end means that its the very last character will be a number, then by simply checking the last character we can prove that the string ends with a number.

# Python3 code to demonstrate how it works
# Check the numeric suffix in the string
# Using isdigit ()

  
# initialization string

test_str = "Geeks4321"

 
# printing the original line

print ( "The original string is:" + str (test_str))

 
# Using isdigit ()
# Check the numeric suffix in the string

res = test_str [ - 1 ]. isdigit ()

 
# print result

print ( "Does string contain suffix number? : " + str (res))

Output:

 The original string is: Geeks4321 Does string contain suffix number?: True 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method