Some String Methods


Previously we've studied String functions - functions that we call like this:
string.function_name(arguments)


Methods are called on objects, like this:
object_name.method_name(arguments)


Example - function call:
string.find("hello", "he")

Example - method call:
s = "hello"
s.isalnum()  # returns true if s is alphanumeric,
                    # i.e., all letters and numbers

String Methods

Method Description
s.isalnum() returns True if s contains only letters and digits
s.isdigit() returns True if s contains only digits
s.isalpha() returns True if s contains only letters
s.isspace() returns True if s contains only whitespace (blanks, new line characters, tabs, etc)
s.islower() returns True if all letters in s are lowercase
s.isupper() returns True if all letters in s are uppercase
s.endswith(suffix) returns True if s ends with suffix
s.startswith(prefix) returns True if s begins with prefix
s.count(x) returns number of times x appears in s
s.strip() returns new string with leading and trailing whitespace characters removed
s.rstrip(str) returns string with trailing characters in str removed
s.lstrip(str) returns string with leading characters in str removed