What's an object? What's a method? Give some examples. →
object - a thing that a variable name can refer to, like a string or integer
for us that means attributes (data) and methods (functions)… all packed into one thing
a method is essentially a function that's associated with a particular object
a methods can be thought of as an action behavior that an object can perform
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? →
Strings Methods!
Strings are objects. They have methods. Lots of 'em!
upper()
lower()
capitalize()
title()
isdigit()
isnumeric()
isalpha()
isspace()
to be continued in next slide! →
Even More String Methods!
find(sub[, start[, end]])
format(…)
strip([chars])
isupper()
islower()
count(…)
replace(…)
In the interactive shell, you could use the dir with a string in parentheses to show all of the methods of an object:
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? →
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 '⅕'.
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? →
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.
strip()
strip() removes leading and trailing whitespace (it can also remove other leading and trailing characters). What do you think this results
format()
Format is like the string formatting operator, but possibly easier?!
instead of %s, use curly braces and the number of the argument as a placeholder {0}, {1}
numer corrseponds to each argument in the call, 0 being the first
What do you think the following returns? →
format() results
isupper() and islower()
isupper() and islower() return True if the string that is called on is the case specified. What does the following output? →
count(), replace()
count(s) …counts the number of times substring, s, occurs in the original string.
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)
A Couple of Exercises:
use upper or lower to check for permutations for input
for example, loop forever
ask the user if they want the loop to stop
accept "Yes", "YES", "yes", etc.
rewrite get_first_word, but use find() instead of a loop