The string type in Python is actually a class, making individual strings objects. Using string methods which belong to the object requires a different syntax than calling a function.
founder:str = "mary lyon"
numYs:int = founder.count( "y" )
print( founder + " has " + str(numYs) + " y's in it" )
yIndex:int = founder.find( "y" )
print( "the first occurrence of the letter y in " + founder \
+ " is at " + str( yIndex ) )
Let’s say we want to generate the capitalized version of a string. Think of that as saying to the string “please tell me what the capitalized version of yourself would look like”. The syntax for that in Python is a dot notation where we put the data first, a dot, and then the method call.
For example, consider this snippet of code:
founder:str = 'mary'
print(founder)
# Print the name with the first letter capitalized.
print(founder.capitalize())
# Let's see if the name variable contains the original value or the capitalized
# value.
print(founder)
The value of variable name does not actually change. Strings are immutable. This means that when we call a method that looks like it might be changing the string, what it is really doing is giving us a new string that contains the change, leaving the original string unmodified. What would you have to do if you wanted to actually change what was stored in name? You would have to assign a new value to it.
Compare the code below to the code above. Run both and make sure you understand why the output is different.
founder:str = 'mary'
print(founder)
# Reassign the name with the first letter capitalized.
founder = founder.capitalize()
# Let's see if the name variable contains the original value or the capitalized
# value.
print(founder)
The following examples show you other things you can do with string methods, including invoking methods directly on a string literal!
# Center text inside a longer string.
text:str = "Mount Holyoke is beautiful!"
print (text.center(40))
# Remove leading and trailing spaces
text2:str = " Life is good! "
print(text2.strip())
# count occurrences of letter i
print("'i' occurs", text.count('i'), "times")
# does the text end with ?
print("Is text a question?", text.endswith('?'))
# is the text all alphanumeric?
print("Is text just letters and numbers?", text.isalnum())
# is the text all letters?
print("Is the text all letters?", text.isalpha())
# are all the letters in the text lower case?
print("Is the text all lowercase?", text.islower())
# show the text in all lower case
print(text.lower())
# show the text in all upper case
print(text.upper())
# replace every i with a u
print(text.replace('i', 'u'))
# find the first i (which is still there because replace didn't change text)
print("The first 'i' is at position", text.find('i'))
# find the first i, starting at the end of the string
print("The last 'i' is at position", text.rfind('i'))
# does the text start with letter a
print("Does the text start with 'a'?", text.startswith('a'))
# does the text start with letter M
print("Does the text start with 'M'?", text.startswith('M'))
# capitalize every word
print(text.title())
The Python documentation is uncomfortably hard to read, but check out these two resources for a list of string methods:
Next: Lists