In this tutorial, we will look at how to check if a string in Python ends with a number (digit) or not with the help of some examples.
How to check if a string ends with a numeric character?
To check if a string in Python ends with a number or not, check if the last character in the string is a digit or not using the string isdigit()
function.
The built-in string isdigit()
function in Python takes a string and returns True
if all the characters in the string are numeric characters and there is at least one character in the string. It returns False
otherwise.
So, use the string isdigit()
function to check if the last character in the string (character at index -1) is a digit. The following is the syntax –
# check if string ends with a number s[-1].isdigit()
It returns True
if the string ends with a numeric character.
Examples
Let’s now look at some examples. First, we will create some strings that we will use throughout this tutorial.
# create strings s1 = "cloud9" s2 = "apple" s3 = "rapper@" s4 = "" # display the strings print(s1) print(s2) print(s3) print(s4)
Output:
cloud9 apple rapper@
Here, we created four strings – s1
, s2
, s3
and s4
. The string s1
ends with a digit, the string s2
ends with an alphabet, the string s3
ends with the special character '@'
and the string s4
is an empty string.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Let’s now check if the strings s1
, s2
, and s3
end with a digit or not.
# check if string ends wtih a number print(s1[-1].isdigit()) print(s2[-1].isdigit()) print(s3[-1].isdigit())
Output:
True False False
We get True
for s1
as it does end with a digit. We get False
for s2
and s3
as they do not end with a digit.
Now let’s apply the above method to the empty string, s4
.
# check if string ends wtih a number print(s4[-1].isdigit())
Output:
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) Input In [4], in <module> 1 # check if string ends wtih a number ----> 2 print(s4[-1].isdigit()) IndexError: string index out of range
We get an IndexError
because we’re trying to access an index that does not exist (an empty string does not have a -1
index).
To avoid the above error, you can first check whether the string is non-empty and then proceed to check if the last character is a digit or not. See the example below –
# function to check if string ends wtih a digit def check_str_ends_with_digit(s): if s and s[-1].isdigit(): return True else: return False # check if string ends wtih a number print(check_str_ends_with_digit(s4))
Output:
False
We get False
as the output for the empty string.
You might also be interested in –
- Python – Check If a Character Appears Twice in String
- Python – Check If String Contains Vowels
- Python – Check If String Contains Lowercase Letters
- Python – Check If String Contains Uppercase Letters
- Python String Uppercase – With Examples
- Python String Lowercase – With Examples
- Python – Capitalize First Letter of Each Word
- Python – Check If All Characters in String are Same
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.