Go to: Na-Rae Han's home page  

Python 3 Notes

        [ HOME | LING 1330/2330 ]

String Methods Part 2

<< Previous Note           Next Note >>
On this page: .index(), .rindex(), .find(), .rfind(), .isalpha(), .isalnum(), .isdigit(), .isspace(), .islower(), .isupper().

Locating a Substring

There is a group of string methods whose function is to return the location of a certain substring: .index(), .rindex(), .find() and .rfind().

Below, .index() returns the index of the first substring found within the string, starting from the left edge:
 
>>> 'green ideas'.index('e') 
2 
>>> 'green ideas'.index('id') 
6
.rindex() is the mirror opposite; it searches from the right edge.
 
>>> 'green ideas'.rindex('e') 
8 
But what if the substring is not found? In such an event, .index() and .rindex() produces an error. Therefore, unless you're sure, it's a good idea to use an in substring test as the precondition:
 
>>> phrase = 'green ideas'
>>> phrase.index('th') 
Traceback (most recent call last):
  File "<pyshell#24>", line 1, in <module>
    phrase.index('th')
ValueError: substring not found  
>>> if 'th' in phrase:
        print(phrase.index('th'))    # nothing prints, but error is avoided

>>> 		
That's where .find() and .rfind() differ from .index() and .rindex(): if the substring is not found, they return -1 instead of producing an error. They behave exactly like .index() and .rindex() otherwise.
 
>>> 'green ideas'.find('e') 
2 
>>> 'green ideas'.rfind('e') 
8
>>> 'green ideas'.find('th')     # No 'th'! Returns -1
-1

Is Every Character ... ?

Some string methods test a string on whether or not every character in it possesses a certain property. For example, .isalpha() returns True if and only if every character in the string is alphabetic, i.e., in a-z or A-Z:
 
>>> 'iPad'.isalpha() 
True 
>>> 'co-operate'.isalpha() 
False 
Below is a list of similar methods and examples. They all return True/False.
Method Is every character ...? Example
.isalpha() alphabetic (A-Z, a-z) See above

.isalnum()

alpha-numeric (A-Z, a-z, 0-9)
>>> 'Homework3'.isalnum() 
True 
>>> 'ice cream'.isalnum() 
False 

.isdigit()

numeric (0-9)
>>> '2014'.isdigit() 
True 
>>> '9:30'.isdigit() 
False 

.isspace()

whitespace character:
' ', '\n', '\t', '\r'
>>> '\n\t  \r  \n'.isspace() 
True 
>>> ' A b c 12 '.isspace() 
False 

.islower()

lowercase (a-z)
>>> 'apple'.islower() 
True 
>>> 'iPad'.islower() 
False 

.isupper()

uppercase (A-Z)
>>> 'HELLO'.isupper() 
True 
>>> 'Hello'.isupper() 
False