CPSC120A
Fundamentals of Computer Science I

Activity 18

Exploring the difference between methods and functions.

String Methods

Strings are not simple data structures like integers and floats. Strings are actually Objects. In addition to the usual data, they also contain functions (called methods), which can work with that data as well. Keeping track of which are methods and which are functions can be a little cumbersome. Instead, you should get used to the types of errors that would result from incorrectly calling methods/functions.


Write how Python would evaluate each of the following expressions. If the expression produces and error, put the error name.

Expression Output
print(len("Hello, World!"))
print(string.len("Hello, World!"))
print("Hello, World".len())
print(count("Hello, World", "l"))
print(string.count("Hello, World", "l"))
print("Hello, World".count("l"))
print(ord("H"))
print(string.ord("H"))
print("H".ord())
print(factorial(5))
print(math.factorial(5))
print(5.factorial())
print(ceil(7.3))
print(7.3.ceil())
print(math.ceil(7.3))
input("What is your favorite color? ")
print("What is your favorite color? ".input())
string.input("What is your favorite color? ")