Lecture 11

String methods

math and random modules

MCS 260 Fall 2021
David Dumas


  • Project 1 due today at 6pm central
  • Homework 4 open, worksheet 4 solutions available
  • Project 2 due Fri 8 Oct


Every value in Python is actually an object: data packaged together with functions that operate on the data.

Functions that are part of an object are called methods.

They are called with a special dot syntax:

        >>> L=[1,2,3]
        >>> L.append(4)  # method of list
        >>> s="sharks with lasers"
        >>> s.split()    # method of str
        ['sharks', 'with', 'lasers']

We've seen a lot of methods so far:

.append(x)add x to end
.insert(i,x)add x at index i
.remove(x)remove first instance of x
.pop()remove and return last element
.index(x)get index of first x in the list
.keys() return iterable of all keys
.values() return iterable of all values
.items() return iterable of key-value pairs
.split() split along whitespace, return list
.lower() convert letters to lowercase

Note: whitespace means consecutive characters that all produce spaces or newlines (tab, space, $\texttt{"\n"}$, ...)

Here are some additional str methods that are useful:

.strip() remove leading and trailing whitespace
.index(x) search for substring x and return index
.upper() convert to uppercase
.isdigit() Boolean: all characters digits?
.isalpha() Boolean: all characters letters?
.isspace() Boolean: all characters whitespace?
.splitlines()Split along newline characters (returns list)
.replace(old,new)Replace each occurrence of old with new.


Let's write a program that takes a passage of text and computes the number of words and the number of distinct words.


The .join() method of a string $\texttt{s}$ takes an iterable as parameter, and concatenates each element of the iterable with $\texttt{s}$ between them.

        >>> s = "+"
        >>> L = ["me","laser-sharks","Shakespeare"]
        >>> s.join(L)
        >>> "".join(L)

Using "".join(iterable) might be the most common case.

.strip(), .split(), .join(), .splitlines(), .replace() are probably the most-used methods for basic string processing.

More on .strip() and .split()

The .strip() method takes an optional parameter, $\texttt{chars}$ which must be a string. It removes any characters from $\texttt{chars}$ from the start and end.

        >>> s = "mathematics"
        >>> s.strip("sam")

The .split() method takes an optional parameter, $\texttt{sep}$, which if given is the delimiter (instead of whitespace).

        >>> s = "spiders and ticks and mites"
        >>> s.split(" and ")
        ['spiders', 'ticks', 'mites']

The math module

The statement

        import math

loads the math module, after which functions and constants in that module can be used in your code, e.g.

            math.sqrt(2)          # square root of 2
            math.exp(-1)          # 1/e
            math.sin(0.5*math.pi) # 1.0

The math module includes all common trig functions, logarithms and exponentials, and the constants pi and e. The the math module docs have a full list.

The random module

The random module, imported with

        import random

includes functions to generate (pseudo)random numbers, e.g.

        >>> random.random()     # Random float in [0,1)
        >>> random.randint(1,6) # Random int between 1 and 6 inclusive
        >>> random.choice( ["Yes", "No", "Maybe"] ) # random item of seq
        >>> L = ["a","b","c","d"]
        >>> random.shuffle(L)   # IN-PLACE shuffle of a list
        >>> L
        ['a', 'd', 'c', 'b']

Pseudorandom numbers

Python uses an equation to generate pseudorandom numbers, starting from some initial data, the seed. By default, the seed is computed from the current time.

So the numbers returned are not random at all!

The PRNG can be manually seeded using random.seed(value).

        >>> random.seed(42)
        >>> random.random()
        >>> random.seed(42)
        >>> random.random()


Revision history

  • 2021-09-16 Initial publication