In this article, discover the power of Python string methods! It explores three essential methods: index(), isalnum(), and isdecimal(). Learn how to find the position of a substring, determine if a string is alphanumeric, and check if a string contains only decimal characters. Enhance your Python programming skills with practical examples and step-by-step explanations.
Before moving ahead, let’s take a look at Python String Methods.
Table of Contents
index() method
index() returns the lowest index number of sub-string found in the string; otherwise, raises an error.
Syntax – string.index(value, start, end)
Parameter values –
value – Required. Search for the specified given string.
start – Optional. Search sub-string from the specified index number.
end – Optional. Specified index number where to end the search.
Note: The index number starts from 0.
Example: Returns the index number of “world”.
text = 'Hello, Python world!'
index_number = text.index('world')
print('Index number of world is:', index_number)
#output:
Index number of world is: 14
Example: Returns the index number of the first occurrence “l”.
text = 'Hello, Python world!'
index_number = text.index('l')
print('Index number of l is:', index_number)
#output:
Index number of l is: 2
Example: Returns the index number of sub-string within a range.
text = 'Hello, Python world!'
index_number = text.index('o', 3, 5)
print("Index number of 'o' is:", index_number)
#output:
Index number of 'o' is: 4
Note: There is only one difference between the index() and find() methods. If the sub-string is not found, index() method returns an error whereas find() method returns -1.
Example: find() method returns -1 if subs-string not found.
text = 'Hello, Python world!'
index_number = text.find('W')
print('Index number of W is:', index_number)
#output:
Index number of W is: -1
isalnum() method
isalnum() returns True, if the string contains –
- only letters a-z.
- only numbers 0-9.
- combination of both a-z and 0-9.
otherwise returns False.
Alphanumeric characters: Alphanumeric characters are the combination of a-z and 0-9.
Non-alphanumeric: Non-alphanumeric contains a mixture of some symbols like #@(space)%!& comma(,) etc.
Syntax – string.isalnum()
Parameter values – No.
text = 'He11o'
is_alnum = text.isalnum()
print('The text is isalnum:', is_alnum)
#output:
The text is isalnum: True
text = 'Hello'
is_alnum = text.isalnum()
print('The text is isalnum:', is_alnum)
#output:
The text is isalnum: True
text = '123456'
is_alnum = text.isalnum()
print('The text is isalnum:', is_alnum)
#output:
The text is is_alnum: True
Example: Check, if the string contains only alphanumeric characters.
text = 'Hello World'
is_alnum = text.isalnum()
print('The text is isalnum:', is_alnum)
#output:
The text is isalnum: False
Explanation: The string contains whitespace between the string ‘Hello’ and ‘World’.
Example: Check, if the string contains only alphanumeric characters.
text = 'Hello World 123'
is_alnum = text.isalnum()
print('The text is alpha:', is_alnum)
#output:
The text is alpha: False
Example: Check, if the string contains only alphanumeric characters.
text = 'Hello@World'
is_alnum = text.isalnum()
print('The text is alnum:', is_alnum)
#output:
The text is alnum: False
isdecimal() method
isdecimal() returns True; if all characters are only numeric (0-9) in the string, else returns False.
Syntax – string.isdecimal()
Parameter Values – No parameter values.
Example: Check if a string contains only decimal characters.
text = '12345'
is_decimal = text.isdecimal()
print('The text is decimal:', is_decimal)
#output:
The text is decimal: True
Example: Check if a string contains only decimal characters.
text = '12345.1'
is_decimal = text.isdecimal()
print('The text is decimal:', is_decimal)
#output:
The text is decimal: False
Example: Check if a string contains only decimal characters.
text = '12345 Hello'
is_decimal = text.isdecimal()
print('The text is decimal:', is_decimal)
#output:
The text is decimal: False
Note: isdecimal() method returns False if using fractions and Roman numerals.
Conclusion
By utilizing these string methods effectively, you can enhance your Python programming skills and simplify various string-related operations. With practical examples and step-by-step explanations, this article equips you with the knowledge to leverage these methods and optimize your code’s performance.
Whether you’re searching for specific substrings, validating input, or checking for decimal characters, the index(), isalnum(), and isdecimal() methods offer versatile solutions for string manipulation and analysis in Python.