Python – Test if Kth character is digit in String

Given a String, check if Kth index is a digit.
Input : test_str = ‘zambiatek9zambiatek’, K = 5
Output : True
Explanation : 5th idx element is 9, a digit, hence True.
Input : test_str = ‘zambiatek9zambiatek’, K = 4
Output : False
Explanation : 4th idx element is s, not a digit, hence False.
Method #1: Using in operator
In this, we create a string of numerics and then use in operator to check if Kth digit lies in that numeric string.
Python3
# Python3 code to demonstrate working of # Test if Kth character is digit in String# Using in operator# initializing stringtest_str = 'zambiatek4zambiatek'# printing original Stringprint("The original string is : " + str(test_str))# initializing KK = 5# checking if Kth digit is string # getting numeric strnum_str = "0123456789"res = test_str[K] in num_str# printing result print("Is Kth element String : " + str(res)) |
The original string is :zambiatek4zambiatek Is Kth element String : True
Method #2 : Using isdigit()
In this, we use inbuilt Py. function to solve this problem, and check if Kth element is digit.
Python3
# Python3 code to demonstrate working of # Test if Kth character is digit in String# Using isdigit()# initializing stringtest_str = 'zambiatek4zambiatek'# printing original Stringprint("The original string is : " + str(test_str))# initializing KK = 5# isdigit checks for digitres = test_str[K].isdigit()# printing result print("Is Kth element String : " + str(res)) |
The original string is :zambiatek4zambiatek Is Kth element String : True
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(1)
Auxiliary Space: O(1)
Method #3 : Using ord() method
Python3
# Python3 code to demonstrate working of# Test if Kth character is digit in String# initializing stringtest_str = 'zambiatek4zambiatek'# printing original Stringprint("The original string is : " + str(test_str))# initializing KK = 5res=Falseif(ord(test_str[K])>=48 and ord(test_str[K])<=57): res=True# printing resultprint("Is Kth element String : " + str(res)) |
The original string is :zambiatek4zambiatek Is Kth element String : True
Method #4 : Using isnumeric() method
Python3
# Python3 code to demonstrate working of# Test if Kth character is digit in String# Using isnumeric()# initializing stringtest_str = 'zambiatek4zambiatek'# printing original Stringprint("The original string is : " + str(test_str))# initializing KK = 5# isdigit checks for digitres = test_str[K].isnumeric()# printing resultprint("Is Kth element String : " + str(res)) |
The original string is :zambiatek4zambiatek Is Kth element String : True
Time Complexity : O(1)
Auxiliary Space : O(1)
Method #5 : Using try-except block
Algorithm:
- Initialize the test_str variable with a given string.
- Print the original string.
- Initialize the K variable with a given value.
- Use try-except block to check if Kth element is a digit.
- If the Kth element is a digit, then set res to True, else False.
- Print the result.
Python3
# initializing stringtest_str = 'zambiatek4zambiatek'# printing original Stringprint("The original string is : " + str(test_str))# initializing KK = 5# using try-excepttry: # checking if Kth element is digit int(test_str[K]) res = Trueexcept ValueError: res = False# printing resultprint("Is Kth element String : " + str(res))#this code is contributed Asif_Shaik |
The original string is :zambiatek4zambiatek Is Kth element String : True
Time complexity: O(1)
The time complexity of this code is O(1) because it only performs a constant number of operations regardless of the length of the input string.
Auxiliary space: O(1)
The auxiliary space complexity of this code is O(1) because it only uses a constant amount of extra space to store the Boolean result.
Method 6 : using regular expressions.
step-by-step approach
- Import the re module to use regular expressions.
- Define a regular expression pattern that matches a single digit. In regular expressions, the \d character class represents a digit. So, our pattern will be r”\d”.
- Use the re.match() function to check if the Kth character matches the pattern. We can access the Kth character using string indexing, like test_str[K]. If the character matches the pattern, the match() function returns a match object, which is a truthy value in Python. If the character does not match the pattern, the function returns None, which is a falsy value in Python.
- Based on the return value of the match() function, set the value of the res variable to True or False.
- Print the result.
Python3
import re# initializing stringtest_str = 'zambiatek4zambiatek'# printing original stringprint("The original string is : " + str(test_str))# initializing KK = 5# regular expression pattern for a single digitpattern = r"\d"# check if the Kth character matches the patternmatch = re.match(pattern, test_str[K])# set the value of res based on the match resultres = bool(match)# printing resultprint("Is Kth element String : " + str(res)) |
The original string is :zambiatek4zambiatek Is Kth element String : True
Time complexity: The time complexity of this method is O(1), as it only involves matching a single character with a regular expression pattern, which takes constant time.
Auxiliary space: The auxiliary space used by this method is also O(1), as it only involves storing a regular expression pattern and a match object, both of which take constant space.


