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

Common xlabel/ylabel for matplotlib subplots

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

NUMPYNUMPY

glob exclude pattern

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

NUMPYNUMPY

Python CSV error: line contains NULL byte

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries