Skip to content

Oct 17: String Slicing, Methods

Learning Objectives

After today's class, you should be able to:

  • Explain the syntax and meaning of slice operations, with and without indexes.
  • Name four methods that strings provide, and describe what each method does.
  • Describe where to find the official documentation for methods of built-in types.

Reminders

POGIL Activity

  • Advanced Strings
    • If you are absent today, complete this activity at home
    • Bring your completed activity to class or office hours

Python Shell in VS Code

VS Code provides a system shell for interacting with the operating system. On Linux, the system shell is usually bash; on macOS, it's usually zsh; and on Windows, it's usually PowerShell. You can't type Python code directly into a system shell. Instead, you can run a Python shell within the system shell.

  • To start a Python shell, type the python command and press Enter.
  • To exit a Python shell, type exit() or quit(). Or use a keyboard shortcut:
    • On Linux and macOS, press Ctrl+D.
    • On Windows, press Ctrl+Z then Enter.

Example Code

Model 1

dna = 'CTGACGACTT'
dna[5]
dna[10]
len(dna)
dna[:5]
dna[5:]
dna[5:10]
triplet = dna[2:5]
print(triplet)
dna[-5]
dna[-10]
dna[:-5]
dna[-5:]
triplet = dna[-4:-1]
print(triplet)

Model 2

dna = 'CTGACGACTT'
dna.lower()
print(dna)
lowercase = dna.lower()
print(lowercase)
dnalist = list(dna)
print(dnalist)
dnalist.reverse()
print(dnalist)
type(dna)
dna = dna.split('A')
print(dna)
type(dna)
dna.replace('C', 'g')
print(dna[0])
type(dna[0])
dna[0].replace('C', 'g')
print(dna)