xiaojuan caicomputational thinking1 lecture 4 computing with strings xiaojuan cai (蔡小娟)...

31
Xiaojuan Cai Computational Thinking 1 Lecture 4 Computing with Strings Xiaojuan Cai 蔡蔡蔡蔡 () [email protected] Fall, 2015

Upload: bennett-lee

Post on 18-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 1

Lecture 4

Computing with Strings

Xiaojuan Cai(蔡小娟)

[email protected]

Fall, 2015

Page 2: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 2

Objective• To understand the string data type and

operations

• To be able to apply string formatting to

produce attractive, informative program

output.

• To understand basic file processing

concepts and techniques.

• To be able to process textual information.

Page 3: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 3

Roadmap

• String data type and operations

• String, lists and sequences

• I/O as string manipulation

• File processing

Page 4: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 4

Try this>>> firstName = input("Please enter your name: ")

Please enter your name: John

•We will get NameError! Why?

•Actually we are running

• firstName = John

• Typing “John”, or use raw_input

Page 5: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 5

String

• Data is text. The type is string.

• A string is a sequence of characters

enclosed within quotation marks (") or

apostrophes (') or (''')

• str1 = “hello”, str2 = ‘hello’

• Str3 = ''' line1

line2'''

Page 6: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 6

Indexing and Slicing

• Indexing: <string>[<expr>]

• returns the character in position

<expr>

• Slicing: <string>[<start>:<end>]

• returns the substring beginning at

<start>, runs up to but doesn’t

include <end>

• The positions are numbered starting with

0.

Page 7: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 7

Indexing and Slicing

• x = “Hello Bob”

• x[0] = ‘H’, x[-9] = ‘H’, x[9],x[-10] out of range

• x[1:5] = “ello”, x[:5] = “Hello”, x[:] = “Hello

Bob”

• x[5:1] = ‘’, x[-1:2] = ‘’, x[2:-1] = “llo Bo”

H e l l o B o b0 1 2 3 4 5 6 7 8

Page 8: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 8

Other operations

•Concatenation: +

•Repetition: *

•Length: len(<string>)

• Iteration: for <var> in <string>

Page 9: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 9

Simple string processing

• Indexing and slicing

• Example: username.py

• Example: month.py

•What if we want to print the

complete month name?

Page 10: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 10

month.pyInput: a number between 1 and 12

Output: the abbreviation for the corresponding month.

Sample output:

1

Jan

5

May

Page 11: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 11

Roadmap

• String data type and operations

• String, lists and sequences

• I/O as string manipulation

• File processing

Page 12: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 12

String and lists• Strings are special kind of sequences.

• Lists are also sequences. All operations on

strings can be applied to lists.

• Strings are sequences of chars, but lists are

sequences of any values.

• [1,’a’, “spam”, 4]

• Lists are mutable, but strings are not.

Page 13: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 13

Representation of strings• Strings are also sequences of 0’s and 1’s

• ASCII (American Standard Code for Information

Interchange): 0~127

• 65-90: A~Z, 97-122: a~z, 48-57:

0~9

• Others: punctuations

• ord() and chr()

• Unicode: 0~216-1nearly all written languages

Page 14: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 14

Strings and secret codes在一堂非常无聊的课上,你想写纸条给你的同学,告诉他(她)“CT is boring…”,但你很怕被老师发现(老师会让你期末考试得0分),于是你想到密码学中的基本加密解密。

1. 和同学确定一个密钥(通常一个明文 -密文对应表格)

2. 加密:将消息中的每个字符转换成相应密文

3. 解密:将密文转换成明文

你和同学约定好用ASCII码来加密。于是你的消息便是:

67 84 32 105 115 32 98 111 114 105 110 103 46 46 46

Page 15: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 15

Strings and secret codes•Encode: text2numbers.py

•Decode: numbers2text.py

• Two useful library functions: split

and eval

•The library string

Page 16: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 16

The usage of string library>>> s = "Hello, I came here for an argument"

>>> string.capitalize(s)

'Hello, i came here for an argument'

>>> string.capwords(s)

'Hello, I Came Here For An Argument'

>>> string.lower(s)

'hello, i came here for an argument'

>>> string.upper(s)

'HELLO, I CAME HERE FOR AN ARGUMENT‘

>>> string.replace(s, "I", "you")

'Hello, you came here for an argument'

>>> string.center(s, 30)

'Hello, I came here for an argument'

Page 17: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 17

The usage of string library>>> string.center(s, 50)' Hello, I came here for an argument

'>>> string.count(s, 'e')5>>> string.find(s, ',')5>>> string.join(["Number", "one,", "the",

"Larch"])'Number one, the Larch'>>> string.join(["Number", "one,", "the",

"Larch"], "foo")'Numberfooone,foothefooLarch'

Page 18: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 18

Roadmap

• String data type and operations

• String, lists and sequences

• I/O as string manipulation

• File processing

Page 19: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 19

Input/Output

•Example: Date convert

• Input: 05/24/2015

• Output: May 24, 2015

•Algorithm: (Quiz)

Page 20: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 20

int() and eval()• int(“05”) returns 5

• eval(“05”) returns 5

• int(“012”) returns 12

• eval(“012”) returns 10

• Even worse, eval(“08”) has syntax error!

• Reason: if an int starts with ‘0’, Python treats it

as a base 8 number.

Page 21: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 21

Number to string and back

Function Meaning

float(<expr>)

Convert expr to a floating point value

int(<expr>) Convert expr to an integer value

long(<expr>)

Convert expr to a long integer value

str(<expr>) Return a string representation of expr

eval(<string>)

Evaluate string as an expression

Page 22: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 22

String format• <template-string> % (<values>)

• % within the template-string mark “slots” into which

the values are inserted.

• There must be one slot per value.

• Each slot has a format specifier that tells Python how

the value for the slot should appear.

• The formatting specifier has the form:

%<width>.<precision><type-char>

Page 23: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 23

Format specifier• %<width>.<precision><type-char>

• <type-char> can be decimal, float, string

• <width> and <precision> are optional.

• <width> tells us how many spaces to use to

display the value. 0 means to use as much

space as necessary.

• <precision> is used with floats, the default is 6

Page 24: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 24

Examples>>> "Hello %s %s, you may have already won $%d" %("Mr.",

“Li", 10000)

'Hello Mr. Li, you may have already won $10000'

>>> 'This int, %05d, was placed in a field of width 5' % (7)

'This int, 7, was placed in a field of width 5'

>>> 'This float, %10.5f, has width 10 and precision 5.' % (3.1415926)

'This float, 3.14159, has width 10 and precision 5.'

>>> 'This float, %0.5f, has width 0 and precision 5.' % (3.1415926)

'This float, 3.14159, has width 0 and precision 5.'

>>> 'Compare %f and %0.20f' % (3.14, 3.14)

'Compare 3.140000 and 3.14000000000000010000'

Page 25: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 25

Roadmap

• String data type and operations

• String, lists and sequences

• I/O as string manipulation

• File processing

Page 26: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 26

Multiple-line strings• A file is a sequence of data that is stored in

secondary memory (disk drive).

• A file usually contains more than one line of

text. Lines of text are separated with a

special character, the newline character.

• In Python, this character is represented as ‘\

n’, just as tab is represented as ‘\t’.

Page 27: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 27

File processing• The process of opening a file involves

associating a file on disk with a variable.

• We can manipulate the file by manipulating

this variable.

• Read from the file

• Write to the file

• When done with the file, it needs to be closed.

Page 28: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 28

File reading• <filevar> = open(<name>, <mode>)

• The mode is either ‘r’ or ‘w’ (reading or writing).

• Infile = open(“numbers.dat”, “r”)

• <filevar>.read()

• <filevar>.readline()

• <filevar>.readlines()

• Examples: printfile.py

Page 29: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 29

File writing• Outfile = open(“mydata.out”, ‘w’)• <filevar>.write(<string>)

• If you open an existing file for writing,

you wipe out the file’s contents.

• If the named file does not exist, a new

one is created.

Page 30: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 31

Coming soon

• Have you noticed the dot notation with the

file variable? infile.read()

• In Python, files are objects.The operations,

called methods, are invoked using this dot

notation.

• Strings and lists are also objects. More on

this later!

Page 31: Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) cxj@sjtu.edu.cn Fall, 2015

Xiaojuan Cai Computational Thinking 32

Conclusion

• String data type and operations it could

have. string library

• Lists and strings are both sequences,

and enjoy some common operations.

• Format the ouput

• File processing and batch processing