# Python – Test if Kth character is digit in String

• Last Updated : 09 Nov, 2022

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

 `# Python3 code to demonstrate working of``# Test if Kth character is digit in String``# Using in operator` `# initializing string``test_str ``=` `'geeks4geeks'` `# printing original String``print``(``"The original string is : "` `+` `str``(test_str))` `# initializing K``K ``=` `5` `# checking if Kth digit is string``# getting numeric str``num_str ``=` `"0123456789"``res ``=` `test_str[K] ``in` `num_str` `# printing result``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

 `# Python3 code to demonstrate working of``# Test if Kth character is digit in String``# Using isdigit()` `# initializing string``test_str ``=` `'geeks4geeks'` `# printing original String``print``(``"The original string is : "` `+` `str``(test_str))` `# initializing K``K ``=` `5` `# isdigit checks for digit``res ``=` `test_str[K].isdigit()` `# printing result``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

 `# Python3 code to demonstrate working of``# Test if Kth character is digit in String` `# initializing string``test_str ``=` `'geeks4geeks'` `# printing original String``print``(``"The original string is : "` `+` `str``(test_str))` `# initializing K``K ``=` `5``res``=``False``if``(``ord``(test_str[K])>``=``48` `and` `ord``(test_str[K])<``=``57``):``    ``res``=``True` `# printing result``print``(``"Is Kth element String : "` `+` `str``(res))`

Output

```The original string is : geeks4geeks
Is Kth element String : True```

My Personal Notes arrow_drop_up