Python – Test if Kth character is digit in String
Given a String, check if Kth index is a digit.
Input : test_str = ‘geeks9geeks’, K = 5
Output : True
Explanation : 5th idx element is 9, a digit, hence True.
Input : test_str = ‘geeks9geeks’, 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
test_str = 'geeks4geeks'
print ( "The original string is : " + str (test_str))
K = 5
num_str = "0123456789"
res = test_str[K] in num_str
print ( "Is Kth element String : " + str (res))
|
Output
The original string is : geeks4geeks
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
test_str = 'geeks4geeks'
print ( "The original string is : " + str (test_str))
K = 5
res = test_str[K].isdigit()
print ( "Is Kth element String : " + str (res))
|
Output
The original string is : geeks4geeks
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
test_str = 'geeks4geeks'
print ( "The original string is : " + str (test_str))
K = 5
res = False
if ( ord (test_str[K])> = 48 and ord (test_str[K])< = 57 ):
res = True
print ( "Is Kth element String : " + str (res))
|
Output
The original string is : geeks4geeks
Is Kth element String : True
Method #4 : Using isnumeric() method
Python3
test_str = 'geeks4geeks'
print ( "The original string is : " + str (test_str))
K = 5
res = test_str[K].isnumeric()
print ( "Is Kth element String : " + str (res))
|
Output
The original string is : geeks4geeks
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
test_str = 'geeks4geeks'
print ( "The original string is : " + str (test_str))
K = 5
try :
int (test_str[K])
res = True
except ValueError:
res = False
print ( "Is Kth element String : " + str (res))
|
Output
The original string is : geeks4geeks
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
test_str = 'geeks4geeks'
print ( "The original string is : " + str (test_str))
K = 5
pattern = r "\d"
match = re.match(pattern, test_str[K])
res = bool (match)
print ( "Is Kth element String : " + str (res))
|
Output
The original string is : geeks4geeks
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.
Last Updated :
24 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...