CS110: Introduction to Computer Science the String Data Type The
Total Page:16
File Type:pdf, Size:1020Kb
The String Data Type CS110: Introduction to Text is represented in programs by the Computer Science string data type. A string is a sequence of characters enclosed within quotation marks (") or Dianna Xu apostrophes ('). 1 2 The String Data Type The String Data Type When you enter a name, it ’s doing the One way to fix this is to enter your same thing as: string input with quotes around it. firstName = Dianna The way Python evaluates expressions Even though this works, this is is to look up the value of the variable cumbersome! Dianna and store it in firstName. 3 4 The String Data Type The String Data Type There is a better way to handle We can access the individual characters text – the raw_input function. in a string through indexing . The positions in a string are numbered raw_input is like input , but it from the left, starting with 0. doesn ’t evaluate the expression The general form is that the user enters. <string>[<expr>] , where the value of expr determines which character is selected from the string. 5 6 Python Programming, 1/e 1 The String Data Type The String Data Type H e l l o B o b H e l l o B o b 0 1 2 3 4 5 6 7 8 0 1 2 3 4 5 6 7 8 In a string of n characters, the last character is at position n-1 since we start counting with 0. We can index from the right side using negative indexes. 7 8 The String Data Type The String Data Type Indexing returns a string containing a Slicing: single character from a larger string. <string>[<start>:<end>] We can also access a contiguous start and end should both be int s sequence of characters, called a The slice contains the substring substring , through a process called beginning at position start and runs up to slicing . but doesn ’t include the position end. If either expression is missing, then the start or the end of the string are used. 9 10 The String Data Type The String Data Type Can we put two strings together into a Operator Meaning longer string? + Concatenation Concatenation “glues ” two strings together ( +) * Repetition <string>[] Repetition builds up a string by multiple Indexing concatenations of a string with itself ( *) <string>[:] Slicing The function len will return the length len(<string>) Length of a string. for <var> in Iteration through <string> characters 11 12 Python Programming, 1/e 2 Simple String Processing Simple String Processing >>> Usernames on a computer system Please enter your first name (all lowercase): john First initial, first seven characters of last Please enter your last name (all lowercase): doe name uname = jdoe # get user ’s first and last names >>> first = raw_input( “Please enter your first name (all lowercase): ”) Please enter your first name (all lowercase): donna last = raw_input( “Please enter your last name (all lowercase): ”) Please enter your last name (all lowercase): rostenkowski uname = drostenk # concatenate first initial with 7 chars of last name uname = first[0] + last[:7] 13 14 Simple String Processing Simple String Processing Another use – converting an int that Month Number Position stands for the month into the three letter Jan 1 0 abbreviation for that month. Feb 2 3 Store all the names in one big string: “JanFebMarAprMayJunJulAugSepOctNovDec” Mar 3 6 Use the month number as an index for Apr 4 9 slicing this string: monthAbbrev = months[pos:pos+3] To get the correct position, subtract one from the month number and multiply by three 15 16 Strings, Lists, and Sequences Strings, Lists, and Sequences Strings are really a special kind of We can use the idea of a list to make sequence , so these operations also our previous month program even apply to sequences! simpler! We change the lookup table for months Strings are always sequences of to a list: characters, but lists can be sequences of arbitrary values. months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", Lists can have numbers, strings, or "Dec"] both! 17 18 Python Programming, 1/e 3 Strings, Lists, and Sequences Strings, Lists, and Sequences To get the months out of the sequence, This version of the program is easy to do this: extend to print out the whole month monthAbbrev = months[n-1] name rather than an abbreviation! Rather than this: months = ["January", "February", "March", "April", "May", monthAbbrev = months[pos:pos+3] "June", "July", "August", "September", "October", "November", "December"] 19 20 Strings, Lists, and Sequences Strings and Secret Codes Lists are mutable , meaning they can be Inside the computer, strings are not changed. Strings can be changed. represented as sequences of 1 ’s and 0’s, just like numbers. A string is stored as a sequence of numbers, one number per character. It doesn ’t matter what value is assigned as long as it ’s done consistently. 21 22 Strings and Secret Codes Strings and Secret Codes In the early days of computers, each 0 – 127 are used to represent the characters manufacturer used their own encoding typically found on American keyboards. of numbers for characters. 65 – 90 are “A”–“Z” 97 – 122 are “a”–“z” Today, American computers use the 48 – 57 are “0”–“9” ASCII system (American Standard Code The others are punctuation and control codes for Information Interchange). used to coordinate the sending and receiving of information. 23 24 Python Programming, 1/e 4 Strings and Secret Codes Strings and Secret Codes One major problem with ASCII is that The ord function returns the it ’s American-centric, it doesn ’t have numeric (ordinal) code of a single many of the symbols necessary for character. other languages. The chr function converts a Newer systems use Unicode , an numeric code to the corresponding alternate standard that includes support character. for nearly all written languages. 25 26 Other String Operations Other String Operations There are a number of other string count( s, sub ) – Count the number of processing functions available in the occurrences of sub in s string library. Try them all! find( s, sub ) – Find the first position capitalize( s) – Copy of s with only the where sub occurs in s first character capitalized join( list ) – Concatenate list of strings capwords( s) – Copy of s; first character into one large string of each word capitalized ljust( s, width ) – Like center, but s is center( s, width ) – Center s in a field of left-justified given width 27 28 Other String Operations Other String Operations lower( s) – Copy of s in all lowercase letters rstrip( s) – Copy of s with trailing lstrip( s) – Copy of s with leading whitespace removed whitespace removed split( s) – Split s into a list of substrings replace( s, oldsub, newsub ) – Replace upper( s) – Copy of s; all characters occurrences of oldsub in s with newsub converted to uppercase rfind( s, sub ) – Like find, but returns the right-most position rjust( s, width ) – Like center, but s is right- justified 29 30 Python Programming, 1/e 5 From Encoding to Encryption The process of encoding information for the purpose of keeping it secret or transmitting it privately is called encryption . Cryptography is the study of encryption methods. 31 Python Programming, 1/e 6.