mr. fowler computer science 14 feb 2011 strings in python

14
Mr. Fowler Computer Science 14 Feb 2011 Strings in Python

Upload: rodger-sparks

Post on 21-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Mr. Fowler Computer Science 14 Feb 2011 Strings in Python

Mr. FowlerComputer Science

14 Feb 2011

Strings in Python

Page 2: Mr. Fowler Computer Science 14 Feb 2011 Strings in Python

Introduction

Prior experience Defining string variables Getting user input Printing strings

Lesson Objectives Understand string structure Access characters and sub-sections from a string Concatenate strings Traverse a string

Page 3: Mr. Fowler Computer Science 14 Feb 2011 Strings in Python

Strings in use

Strings are generally used to store text data sample=“LEVEL” name=raw_input (“What is your name?”)

Can also store non-natural language data e.g. telephone numbers

my_tel=“+1 (301) 294 4444” my_speed=“300 m/s”

Q: What is the difference between strings and integers?

Page 4: Mr. Fowler Computer Science 14 Feb 2011 Strings in Python

Strings in use

Strings are generally used to store text data sample=“LEVEL” name=raw_input (“What is your name?”)

Can also store non-natural language data e.g. telephone numbers

my_tel=“+1 (301) 294 4444”

Q: What data should (not) be stored in strings?

Page 5: Mr. Fowler Computer Science 14 Feb 2011 Strings in Python

String composition

Strings are a composite data type Built from another data type, characters In a string, characters are stored in sequence. Strings can be any length (or empty). String constants are enclosed in double quotes.

str_var = “300 m/s” empty_str=“”

Page 6: Mr. Fowler Computer Science 14 Feb 2011 Strings in Python

String length

Use len() to return the length of a string sample=“SERIES” len(sample)=

empty_str=“” len(empty_str) =

Page 7: Mr. Fowler Computer Science 14 Feb 2011 Strings in Python

String representation

In strings, characters are accessed by index …like mailboxes in an apartment building.

First index is 0, not 1. s=“LEVEL” startChar=s[0] just_v=s[ ]

Python strings are immutable (can’t change characters). s[0]=“B”

Try it out

Page 8: Mr. Fowler Computer Science 14 Feb 2011 Strings in Python

String subsections

Use a range to specify a slice (sub-string) from start index up to but not including the last index speed_display = “300 m/s” middle_two_characters= speed_display[1:3]

Omit the first value to select the start of a string just_num= speed_display[:_]

Omit the second value to select the end of a string

just_unit = speed_display[_:]

Try it out

Page 9: Mr. Fowler Computer Science 14 Feb 2011 Strings in Python

String operations: Concatenation

Combine strings using + (concatenation operator) full_name = “Henry” + “ “ + “James” print “:” + full_name + ”:”

Concatenation is not addition vision_str=“20”+”20” vision_val=20+20

Try building a string build=“” while len(build)<5: build = build +”a” print build

Page 10: Mr. Fowler Computer Science 14 Feb 2011 Strings in Python

String comparison

To test for equality, use the == operatoruser_name=raw_input(“Enter your username?”)

if user_name==“Brad”:print “Welcome back Bradley”

To compare order, use the < and > operators if user_name<“Sam”:

print “Your name is before Sam’s in the phone book”

These operators are case sensitive.Upper case characters are ‘less than’ lower

caseTry it out

Page 11: Mr. Fowler Computer Science 14 Feb 2011 Strings in Python

Traversing through a string

Use a loop to examine each character in a stringstrng=“count the number of u’s in this string”

index = 0

count=0

while index < len(strng):

if strng[index] == “u”

count+=1

How would we traverse backwards?Try it outSee HTTLCS for an alternative format: for in

Page 12: Mr. Fowler Computer Science 14 Feb 2011 Strings in Python

Summary

Strings are composed of characterslen(sample) :String length sample[i] :Character at index i (starts at 0)sample[start:end] :Slice from start up to but

not including end indexsample+sample : Concatenate stringssample==“test” : Test for equalitysample<test: Test for orderMore details and exercises: HTLLCS Ch 7

Page 13: Mr. Fowler Computer Science 14 Feb 2011 Strings in Python

Advanced Strings

Page 14: Mr. Fowler Computer Science 14 Feb 2011 Strings in Python

String operations: find

find() searches for a string within a stringTo use it, insert this at the top of your code:

import string

find() returns the first index of a substring full_name = “Henry James” string.find(full_name,”e”)

You can specify the starting point of the search:string.find(full_name,”e”,2)

If the string is not found, find() returns -1find() is case sensitive