Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Strings - Introduction to Computer Science I - Lecture Slides | CS 110, Study notes of Computer Science

Material Type: Notes; Class: Intro to Computer Science I; Subject: Computer Science; University: University of San Francisco (CA); Term: Spring 2007;

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-6nr
koofers-user-6nr 🇺🇸

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Strings
pf3
pf4
pf5
pf8

Partial preview of the text

Download Strings - Introduction to Computer Science I - Lecture Slides | CS 110 and more Study notes Computer Science in PDF only on Docsity!

Strings

Strings

• A string is a series of^ characters

• Characters can be referenced by using

brackets

• The first character is at position 0

mystring = “the”

letter = mystring[2] #letter becomes ‘e’

mystring t h e

0 1 2

for loops

mystring = "CS is cool!"

for c in mystring:

print c

index=

while index < len(mystring):

print mystring[index]

index += 1

Slices

• Select a segment of a string

• Specify [start:end]

– include start but do not include end

– if you do not specify start slice starts from the

beginning

– if you do not specify end slices goes to end

mystring=“ CS is cool” print mystring[6:10] print mystring[2:7] print mystring[:4] print mystring[:]

Immutability

• Strings are immutable

– they cannot be changed

string module

• Contains useful methods for strings

http://docs.python.org/lib/string methods.html

• Dot notation allows us to call a method on

a string object

import string mystring=“ adam” string.find(mystring, “ a” ) #returns index of first instance found mystring=“ CS is cool” mystring.split() #result [‘ CS’ ,’ is’ ,’ cool’ ] newstring = mystring.replace(“ CS” , “ Econ” )