String Objects, String Methods

Objects and Methods (Again!)

What's an object? What's a method? Give some examples. →

Calling Methods

How do you call a method? For example, if you had a string named food, that contains the value "pizza", how would you call the upper method on it to tell it to give back an uppercase version of itself? →

food = "pizza"
#  tell food to give back an uppercase version of itself
food.upper()
#  use the object name
#  followed by dot
#  and the method (from here, it's like a regular function)
#  notice that upper has no arguments

Strings Methods!

Strings are objects. They have methods. Lots of 'em!

  1. upper()
  2. lower()
  3. capitalize()
  4. title()
  5. isdigit()
  6. isnumeric()
  7. isalpha()
  8. isspace()

to be continued in next slide!

Even More String Methods!

  1. find(sub[, start[, end]])
  2. format(…)
  3. strip([chars])
  4. isupper()
  5. islower()
  6. count(…)
  7. replace(…)


In the interactive shell, you could use the dir with a string in parentheses to show all of the methods of an object:

dir("some string")

Casing Methods

upper(), lower(), capitilize(), and title() return the string that the method was called on as all uppercase, all lowercase, first letter uppercase, and title-cased (first letter of every word uppercase). What would the following print out? →

print("this should be uppercase".upper())
print("THIS SHOULD BE LOWERCASE".lower())
print("this should be uppercase".capitalize())
print("this should be uppercase".title())
THIS SHOULD BE UPPERCASE
this should be lowercase
This should be uppercase
This Should Be Uppercase

isdigit(), isnumeric() and isalpha()

isdigit(), isnumeric() and isalpha() test whether a string is only composed of all numbers or all letters (all three return False if empty string). What would the following print out? →

* isnumeric() also returns true for numeric characters other than 0-9, such as '⅕'.

print("123".isdigit())            # True
print("1.23".isdigit())           # False (. is not 0 - 9)
print("one two three".isdigit())  # False (not 0 - 9)
print("onetwothree".isalpha())    # True
print("one two three".isalpha())  # False (has spaces)
print("one!".isalpha())           # False (has !)
print("1".isalpha())              # False (it's a digit)
print("⅕".isdigit())              # False (not 0 - 9)
print("⅕".isnumeric())  # True (isnumeric allows other numeric chars)

isspace()

isspace() gives back true if all of the characters in the string it's called on is white space - any kind of white space. What is the output of the following?

print("             ".isspace())
print("\n".isspace())
print("some    space".isspace())
True
True
False

find()

find() returns the first index where the argument (a character or substring) is found. It returns -1 if the substring is not in the original string.

print("hello".find("h"))
print("hello".find("x"))
print("hello".find("lo"))
0
-1
3

strip()

strip() removes leading and trailing whitespace (it can also remove other leading and trailing characters). What do you think this results

print("  spaces all around   ".strip())
spaces all around

format()

Format is like the string formatting operator, but possibly easier?!

"{0} elephants".format("twenty")
"{0} elephants".format(20)
"{0} elephants".format(20, 100)
"{1} elephants".format(20, 100)
"{0} elephants and {1} peanuts".format(20, 100)

format() results

twenty elephants
20 elephants
20 elephants
100 elephants
20 elephants and 100 peanuts

isupper() and islower()

isupper() and islower() return True if the string that is called on is the case specified. What does the following output?

print("this should be uppercase".isupper())
print("THIS SHOULD BE LOWERCASE".isupper())
False
True

count(), replace()

count(s) …counts the number of times substring, s, occurs in the original string.

'aardvark'.count('a') # --> 3

replace(s, new_s) …replaces all occurrences of substring, s, with new_s. (note that this gives back a new string, and it does not change the original)

'aardvark'.replace('a', '42') # --> 4242rdv42rk

A Couple of Exercises: