In This Article You Will Learn About Python String Method index(), isalnum() and isdecimal()
Python String Method – Before moving ahead, let’s know a little bit about methods expandtabs(), find() and format()
index method() – It returns the index number of specified searched string. It raises an error if specified string not found. In other words, it returns the lowest index number of sub-string.
Syntax - string.index(value, start, end) Parameter values – value - It is required. Search for specified given string. start - It is optional. Specified index number from where to search. end - It is optional. Specified index number where to end search.
Note: In Python, counting starts from 0.
Example 1- To check the index number of sub-string or phrase.
x = 'Hello, Python world!' y = x.index('Python') print('Index number of Python is:', y)

Example 2- To check the index number of first occurrence of sub-string.
x = 'python in python' y = x.index('h') print("first occurrence of 'h' is:", y)

Example 3- To check the index number of first occurrence sub-string from start to end.
x = 'Hello, Python world!' y = x.index('o', 5, 18) print(y)

Note: The index() method is almost same as find() method but the only one difference is that if character doesn’t contain in given string, index() method raises value error whereas find() method returns -1.
Example 4- If sub-string not found, index() method raises an error (value error) but find() method returns -1.
x = 'python in python' z = x.find('K') #index() method y = x.index('K') #find() method print(z) print(y)

isalnum() method – It returns True if given string contains all alphanumeric character.
Alphanumeric – It contains combination of both, letters (a-z) and numbers (0-9).
Non-alphanumeric – It contains mixture of some symbols like #@(space)%!& comma(,) etc.
Syntax - string.isalnum() Parameter values - No parameter values.
Example 1- To check if all characters in given string are alphanumeric.
x = 'python1234' z = x.isalnum() print(z)

Example 2- To check if all characters in given string are not alphanumeric.
x = 'python is a programming language' z = x.isalnum() print(z)

Example 3- To check if all characters in given string are alphanumeric or not after including non-alphanumeric characters.
x = 'python 1234' z = x.isalnum() print(z)

isdecimal() method – It returns True if all the characters are decimals (0-9) in given string otherwise False.
Syntax - string.isdecimal() Parameter Values - No parameter values.
Example 1- To check given character is decimal.
x = '64772' y = x.isdecimal() print(y)

Example 2- Check if all given characters is decimal with including alphabet.
x = '7573agsvf’ y = x.isdecimal() print(y)

Note: – isdecimal() method gives False if we use fraction and roman numerals.
If you find anything incorrect in above discussed topic and have any further question, please comment down below.
Like us on