strings string text as a value
play

Strings String: Text as a Value String are quoted characters Type - PowerPoint PPT Presentation

Mini-Lecture 5 Strings String: Text as a Value String are quoted characters Type : str 'abc d' (Python prefers) "abc d" (most languages) How to write quotes in quotes? Char Meaning \' single quote Delineate with


  1. Mini-Lecture 5 Strings

  2. String: Text as a Value • String are quoted characters Type : str § 'abc d' (Python prefers) § "abc d" (most languages) • How to write quotes in quotes? Char Meaning \' single quote § Delineate with “other quote” \" double quote § Example : " ' " or ' " ' \n new line § What if need both " and ' ? \t tab • Solution : escape characters \\ backslash § Format: \ + letter § Special or invisible chars 9/5/18 Strings 2

  3. String are Indexed • s = 'abc d' • s = 'Hello all' 0 1 2 3 4 0 1 2 3 4 5 6 7 8 a b c d H e l l o a l l • What is s[3:6] ? • Access characters with [] § s[0] is 'a' A: 'lo a' § s[4] is 'd' B: 'lo' § s[5] causes an error C: 'lo ' § s[0:2] is 'ab' (excludes c ) D: 'o ' § s[2:] is 'c d' E: I do not know • Called “string slicing” 9/5/18 Strings 3

  4. String are Indexed • s = 'abc d' • s = 'Hello all' 0 1 2 3 4 0 1 2 3 4 5 6 7 8 a b c d H e l l o a l l • What is s[3:6] ? • Access characters with [] § s[0] is 'a' A: 'lo a' § s[4] is 'd' B: 'lo' § s[5] causes an error C: 'lo ' CORRECT § s[0:2] is 'ab' (excludes c ) D: 'o ' § s[2:] is 'c d' E: I do not know • Called “string slicing” 9/5/18 Strings 4

  5. String are Indexed • s = 'abc d' • s = 'Hello all' 0 1 2 3 4 0 1 2 3 4 5 6 7 8 a b c d H e l l o a l l • What is s[:4] ? • Access characters with [] § s[0] is 'a' A: 'o all' § s[4] is 'd' B: 'Hello' § s[5] causes an error C: 'Hell' § s[0:2] is 'ab' (excludes c ) D: Error! § s[2:] is 'c d' E: I do not know • Called “string slicing” 9/5/18 Strings 5

  6. String are Indexed • s = 'abc d' • s = 'Hello all' 0 1 2 3 4 0 1 2 3 4 5 6 7 8 a b c d H e l l o a l l • What is s[:4] ? • Access characters with [] § s[0] is 'a' A: 'o all' § s[4] is 'd' B: 'Hello' § s[5] causes an error C: 'Hell' CORRECT § s[0:2] is 'ab' (excludes c ) D: Error! § s[2:] is 'c d' E: I do not know • Called “string slicing” 9/5/18 Strings 6

  7. Other Things We Can Do With Strings • Operation in : s 1 in s 2 • Function len: len(s) § Tests if s 1 “a part of” s 2 § Value is # of chars in s § Say s 1 a substring of s 2 § Evaluates to an int § Evaluates to a bool • Examples : • Examples : § s = 'abracadabra' § s = 'abracadabra’ § 'a' in s == True § len(s) == 11 § 'cad' in s == True § len(s[1:5]) == 4 § 'foo' in s == False § s[1:len(s)-1] == 'bracadabr' 9/5/18 Strings 7

  8. String Functions • The introcs module has several string functions § Installed as part of Cornell Extensions § http://cs1110.cs.cornell.edu/docs/strings.html • Use these instead of methods for now § Methods are an advanced programming feature § You will see them on Stack Overflow § Will come back to methods later • Will need these functions for Assignment 1 9/5/18 Strings 8

  9. Examples of String Functions • introcs.index_str(s 1 ,s 2 ) • s = 'abracadabra’ § Position of the first • from introcs import * instance of s 2 in s 1 • index_str(s,'a') == 0 • index_str(s, 'rac') == 2 • introcs.count_str(s 1 ,s 2 ) • count_str(s, 'a') == 5 § Number of times s 2 appears inside of s 1 • count_str(s, 'x') == 0 • strip(' a b ') == 'a b' • introcs.strip(s) § A copy of s with white- See IntroCS space removed at ends Docs for more 9/5/18 Strings 9

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend