Presentation is loading. Please wait.

Presentation is loading. Please wait.

Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) Fall, 2015.

Similar presentations


Presentation on theme: "Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) Fall, 2015."— Presentation transcript:

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

2 Xiaojuan CaiComputational Thinking2 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.

3 Xiaojuan CaiComputational Thinking3 Roadmap String data type and operations String, lists and sequences I/O as string manipulation File processing

4 Xiaojuan CaiComputational Thinking4 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

5 Xiaojuan CaiComputational Thinking5 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'''

6 Xiaojuan CaiComputational Thinking6 Indexing and Slicing Indexing: [ ] returns the character in position Slicing: [ : ] returns the substring beginning at, runs up to but doesn’t include The positions are numbered starting with 0.

7 Xiaojuan CaiComputational Thinking7 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” HelloBob 0 1 2 3 4 5 6 7 8

8 Xiaojuan CaiComputational Thinking8 Other operations Concatenation: + Repetition: * Length: len( ) Iteration: for in

9 Xiaojuan CaiComputational Thinking9 Simple string processing Indexing and slicing Example: username.py Example: month.py What if we want to print the complete month name?

10 Xiaojuan CaiComputational Thinking10 month.py Input: a number between 1 and 12 Output: the abbreviation for the corresponding month. Sample output: 1 Jan 5 May

11 Xiaojuan CaiComputational Thinking11 Roadmap String data type and operations String, lists and sequences I/O as string manipulation File processing

12 Xiaojuan CaiComputational Thinking12 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.

13 Xiaojuan CaiComputational Thinking13 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~2 16 -1nearly all written languages

14 Xiaojuan CaiComputational Thinking14 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

15 Xiaojuan CaiComputational Thinking15 Strings and secret codes Encode: text2numbers.py Decode: numbers2text.py Two useful library functions: split and eval The library string

16 Xiaojuan CaiComputational Thinking16 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'

17 Xiaojuan CaiComputational Thinking17 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'

18 Xiaojuan CaiComputational Thinking18 Roadmap String data type and operations String, lists and sequences I/O as string manipulation File processing

19 Xiaojuan CaiComputational Thinking19 Input/Output Example: Date convert Input: 05/24/2015 Output: May 24, 2015 Algorithm: (Quiz)

20 Xiaojuan CaiComputational Thinking20 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.

21 Xiaojuan CaiComputational Thinking21 Number to string and back FunctionMeaning float( )Convert expr to a floating point value int( )Convert expr to an integer value long( )Convert expr to a long integer value str( )Return a string representation of expr eval( )Evaluate string as an expression

22 Xiaojuan CaiComputational Thinking22 String format % ( ) % 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: %.

23 Xiaojuan CaiComputational Thinking23 Format specifier %. can be decimal, float, string and are optional. tells us how many spaces to use to display the value. 0 means to use as much space as necessary. is used with floats, the default is 6

24 Xiaojuan CaiComputational Thinking24 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'

25 Xiaojuan CaiComputational Thinking25 Roadmap String data type and operations String, lists and sequences I/O as string manipulation File processing

26 Xiaojuan CaiComputational Thinking26 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 ’.

27 Xiaojuan CaiComputational Thinking27 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.

28 Xiaojuan CaiComputational Thinking28 File reading = open(, ) The mode is either ‘r’ or ‘w’ (reading or writing). Infile = open(“numbers.dat”, “r”).read().readline().readlines() Examples: printfile.py

29 Xiaojuan CaiComputational Thinking29 File writing Outfile = open(“mydata.out”, ‘w’).write( ) 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.

30 Xiaojuan CaiComputational Thinking30 Example: Batch processing Batch mode processing is where program input and output are done through files. create usernames for a computer system where the first and last names come from an input file. userfile.py

31 Xiaojuan CaiComputational Thinking31 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!

32 Xiaojuan CaiComputational Thinking32 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


Download ppt "Xiaojuan CaiComputational Thinking1 Lecture 4 Computing with Strings Xiaojuan Cai (蔡小娟) Fall, 2015."

Similar presentations


Ads by Google