If you can't read please download the document
Upload
wyome
View
32
Download
0
Embed Size (px)
DESCRIPTION
Strings in Python. Computer Science 18 Feb 2011. 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. - PowerPoint PPT Presentation
Citation preview
Computer Science18 Feb 2011Strings in Python
IntroductionPrior experienceDefining string variablesGetting user input Printing stringsLesson ObjectivesUnderstand string structureAccess characters and sub-sections from a stringConcatenate stringsTraverse a string
Strings so farStrings are generally used to store text data sample="LEVEL" Name=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/should not be stored in strings?
Strings vs. IntegersBoth strings and integer variables can store numbersmy_speed_str="300"my_speed_integer=300 Q: What is the difference between strings and integers?
Strings in useQ: Where are strings displayed and entered?
String compositionStrings are a composite data typeBuilt from another data type, charactersIn a string, characters are stored in sequence.Strings can be any length (including empty).String are enclosed in quotation marks.str_var = "300 m/s"empty_str=""
Length of a stringUse len(str) to return string length (# of characters)sample="SERIES" len(sample)= empty_str=""len(empty_str) =
06
String representationIn strings, characters are accessed by index like mailboxes in an apartment building. First index is 0, not 1. s="LEVEL"startChar=s[ ] just_v=ss[ ]
Python strings are immutable (cant change individual chars) s[0]="B"Try it out20
Use a range to specify a slice (sub-string)from start index up to but not including the last indexspeed_display = "300 m/s zeroZero = speed_display[ : ] Omit the first value to select the start of a stringjust_num= speed_display[: ]
Omit the second value to select the end of a stringjust_unit = speed_display[ :] Try it outSlices (sections of a string)31 34
Operations: ConcatenationCombine strings using + (concatenation operator)full_name = "Henry" + " " + "James"print ":" + full_name + ":"
Concatenation is not additionvision_str="20"+"20" vision_val=20+20
Try it out: Build a stringbuild=""while len(build)
Operations: Traversing through a stringUse a loop to examine each character in a stringSee HTTLCS for an alternative format: for in strng="count # of us" index=0count=0while index < len(strng): if strng[index] == "u or strng[index] == U":count+=1index+=1Try it outHow would we traverse backwards?
Advanced Strings
String operations: findfind() searches for a string within a stringTo use it, insert this at the top of your code:import stringfind() returns the first index of a substringfull_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
Why is this useful? Check format (e.g. filename characters), find/ replace (in word processor), or parse document HTML to create a documentStrings: URLs, bar codes, names, documents, labelsNon strings: numbers-integers (scores), floats (prices), booleans, media (graphics) List in two columns on the board.Strings: Mix text and numbers. Composed of series of characters. Can order alphabetically. Constants have quotes around them.Integers: Only numbers and pos/neg sign. Can perform arithmetic. Cant decompose into smaller part. Can compare numerically. Take up less space. Different binary representationStrings: URLs, bar codes, names, documents, labelsNon strings: numbers-integers (scores), floats (prices), booleans, media (graphics) On your computer: create a string with your first name, then print the first and last characters, and the length of the string. Experiment with indicies that are out of range (beyond length) and negative. Try out: specify a string with three words in it, then print the first, second and third words using slices.Concatenation is different from integer addition, also they use the same symbol. Note: a space still counts as a character, but can be hard to see on screen sometimes we print strings with a colon on each side to make spaces clear. *Works for both strings and individual charactersTry it out: compare your name with a capital at the start or not. Try with a space at the start or not. Try comparing order