Python String Method index(), isalnum(), isdecimal()

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)
python-string-method
As it is shown clearly it returned index number of ‘Python’ 7 after counting also for comma and whitespace.

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)
python-string-method
As it is shown clearly that it returned index number of lowest sub-string ‘h’ that is 3.

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)
python-string-method
As it shown clearly that letter ‘o’ found in range and returned index number 11.

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)
python-string-method
As it is shown clearly that find() method did not get letter ‘K’ therefore it returned ValueError.

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)
python-string-method
As it is shown that variable contains alpha-numeric character, therefore it returned True.

Example 2- To check if all characters in given string are not alphanumeric.

x = 'python is a programming language'
z = x.isalnum()

print(z)
python-string-method
As it is shown that variable contains non-alphanumeric character because it has whitespace between characters, therefore it returned False.

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)
python-string-method
As it is shown that variable contains alphanumeric characters but with whitespace, therefore it returned False.

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)
python-string-method
As it is shown that variable contains decimal number therefore, it returned True.

Example 2- Check if all given characters is decimal with including alphabet.

x = '7573agsvf’
y = x.isdecimal()

print(y)
python-string-method
As it is shown that variable does not contain only decimal number, therefore it returned False.

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

1 thought on “Python String Method index(), isalnum(), isdecimal()”

Leave a Comment

Stay in the loop

codingstreets