Presentation is loading. Please wait.

Presentation is loading. Please wait.

Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.

Similar presentations


Presentation on theme: "Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions."— Presentation transcript:

1 Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions. –String manipulation.

2 Today… Strings: –What do we already know about strings? –Characters. –Start String methods. Winter 2016CISC101 - Prof. McLeod2

3 3 Strings String manipulation is a frequent activity in any programming language: –Web site construction / analysis. –Speech analysis. –Text searching and indexing. –Spell and grammar checkers. –Program (code) interpretation. –Scanning Emails for SPAM. –NSA… A string is a kind of data structure – like a tuple, but they have lots of methods (tuples don’t)! Winter 2016

4 CISC101 - Prof. McLeod4 Strings, so Far… What do we know, so far? String literals: "Hello there! ", or 'Hey man!', or """Multiline string""" You can store a string in a variable, just like anything else. They are of type str. A conversion BIF to create strings: str( ). Winter 2016

5 CISC101 - Prof. McLeod5 Strings, so Far, Cont. A BIF to input a string from the keyboard: input( ) You can put escape sequences like: \n, \”, \’, \\, \t in a string to control how a string is displayed. The string format() method is used to help format numeric output for display. Winter 2016

6 CISC101 - Prof. McLeod6 Strings, so Far, Cont. You can concatenate strings using +. You can generate repeating strings using *. You can compare strings using ==, >, =, <= and != (just like comparing numbers, but you must have a string on both sides of the operator). Strings are compared on the basis of the ASCII code values for their individual characters. Winter 2016

7 Characters, so Far The Unicode character value takes two bytes of memory for a value between 0x0000 and 0xFFFF. Python uses Unicode already. The ASCII system only uses one byte. Keyboard characters form the lower half of the ASCII character set. “Extended ASCII” characters occupy the upper half. Strings are immutable collections of characters. Winter 2016CISC101 - Prof. McLeod7

8 8 Strings, so Far, Cont. Like other collections we have found that the following also work with strings: –The slice operator [ : ] –in and not in work with strings as long as a string is placed on both sides. –for loops work with strings. –len() works with strings. –list() and tuple() will create a list or a tuple consisting of the individual characters from the string. –sorted() returns a sorted list of individual characters. –reversed() and enumerate() also work (along with a for loop). (See StringDemo.py) Winter 2016

9 CISC101 - Prof. McLeod9 Strings vs Tuples Strings are immutable like Tuples: –Cannot put the slice operator on the left side of the assignment operator. –del does not work. But, Tuples only have two methods – count() and index(). Strings have many methods… Winter 2016

10 Characters What strings are made of! In Python, a character literal is just a string of length one. A Character has to be stored as a numeric value in RAM (how else?). So, we need to know which numeric values stand for which characters. Thus the Unicode system which needs a two-byte number to store the character’s numeric code. CISC101 - Prof. McLeod10Winter 2016

11 CISC101 - Prof. McLeod11 Character BIFs chr() takes an integer argument, which is the Unicode number and returns the corresponding character. This number can range from 0x0000 to 0xFFFF (65535 10, 2 bytes, compared to ASCII’s 1 byte). ord() does the reverse of chr() returning the character code value, given a single character as a string. Winter 2016

12 CISC101 - Prof. McLeod12 String Methods Just like lists, a string object owns many methods (at least 44). Lots of them (35 – a subset – are listed here)! As Douglas Adams would say: String method signatures will be provided with any question needing these methods. Or the Python Aid Sheet now linked to the main page of the course web site will be provided. Winter 2016

13 String Methods, Cont. How can you see a list of all the string methods? First: 3 slides going through the methods in alphabetical order without any other description. Note the use of default arguments. Remember that they are invoked as in: string_variable.method_name() CISC101 - Prof. McLeod13Winter 2016

14 CISC101 - Prof. McLeod14 string.capitalize() string.center(width) string.count(str, beg=0, end=len(string)) string.endswith(obj, beg=0, end=len(string)) string.expandtabs(tabsize=8) string.find(str, beg=0, end=len(string)) string.format(args) string.index(str, beg=0, end=len(string)) string.isalnum() string.isalpha() string.isdigit() string.islower() string.isspace() string.istitle() Winter 2016

15 CISC101 - Prof. McLeod15 string.isupper() string.join(seq) string.ljust(width) string.lower() string.lstrip() string.partition(str) string.replace(str1, str2, num=string.count(str1)) string.rfind(str, beg=0, end=len(string)) string.rindex(str, beg=0, end=len(string)) string.rjust(width) string.rpartition(str) string.rstrip() string.split(str=“ “, num=string.count(str)) Winter 2016

16 CISC101 - Prof. McLeod16 string.splitlines(num=string.count(‘\n’)) string.startswith(obj, beg=0, end=len(string)) string.strip() string.swapcase() string.title() string.translate(str, del=““) string.upper() string.zfill(width) Winter 2016

17 CISC101 - Prof. McLeod17 String Methods, Cont. Each method returns something. None of them alter the string object (strings are immutable!). Next slides: Categorize by return value: –boolean ( True or False ) –integer –another string –a list or tuple of strings Winter 2016

18 CISC101 - Prof. McLeod18 Boolean Returns string.endswith(obj, beg=0, end=len(string)) Returns True if string has obj at the end of the string or False otherwise. obj is usually a string, but can be a tuple of strings. If it is a tuple True will be returned if any one of the strings match. You have the option of limiting the search to a portion of string. string.startswith(obj, beg=0, end=len(string)) Just like endswith(), but looks at the start of string instead. Winter 2016

19 CISC101 - Prof. McLeod19 Boolean Returns - the “is…” Ones string.isalnum() Returns True if all of the characters in string are alphanumeric (letters and numbers, only), False otherwise. string.isalpha() True if all alphabetic (letters only) string.isdigit() True if all digits (numbers only) string.islower() True if all letters are lower case string.isspace() True if only whitespace: tabs, spaces… string.istitle() True if “titlecased” string.isupper() True if letters are all upper case Winter 2016

20 CISC101 - Prof. McLeod20 Aside - “Titlecase” Is When All Words In The String Start With A Capital Letter And All Other Letters Are Lower Case. string.title() Will return a version of string that is in Titlecase. Winter 2016

21 CISC101 - Prof. McLeod21 Integer Returns string.count(str, beg=0, end=len(string)) Returns a count of how many times str occurs in string, or a substring of string as specified by beg and end. Winter 2016

22 CISC101 - Prof. McLeod22 Integer Returns, Cont. string.find(str, beg=0, end=len(string)) string.index(str, beg=0, end=len(string)) Returns the location of the first occurrence of str in string starting the search from the beginning of the string, or searches a substring specified by beg and end. find() returns -1 if not found, index() raises an exception if not found. string.rfind(str, beg=0, end=len(string)) string.rindex(str, beg=0, end=len(string)) Same as above but searches string from the end. Winter 2016

23 CISC101 - Prof. McLeod23 String Returns string.capitalize() Returns a string that is the same as string except the first letter is capitalized. string.lower() Returns a string that has all the upper case letters in string converted to lower case. string.swapcase() Inverts case for all letters in string. string.upper() Converts all lower case letters in string to upper case. Winter 2016

24 CISC101 - Prof. McLeod24 String Returns, Cont. string.center(width) Adds spaces to string to centre it in a column of size width. string.ljust(width) Adds spaces to string to left justify it in a column of size width. string.rjust(width) Adds spaces to string to right justify it in a column of size width. Winter 2016

25 CISC101 - Prof. McLeod25 String Returns, Cont. string.expandtabs(tabsize=8) Returns a version of string that has all the tab characters converted to spaces, where the default is 8 spaces per tab. string.join(seq) Joins all string representations of the elements in the list, seq, together using string as the separator. Winter 2016

26 CISC101 - Prof. McLeod26 String Returns, Cont. string.lstrip() Removes all leading whitespace (tabs, spaces, linefeeds, etc.) in string. string.rstrip() Removes all trailing whitespace in string. string.strip() Removes all leading and trailing whitespace in string. Winter 2016

27 CISC101 - Prof. McLeod27 String Returns, Cont. string.replace(str1, str2, num=string.count(str1)) Replaces all (or num) occurrences of str1 in string with str2. string.format(args) We’ve used this one already! The supplied arguments are formatted according to the “replacement fields” contained in the string itself. Winter 2016

28 CISC101 - Prof. McLeod28 Tuple or List Returns string.partition(str) Carries out a find() and then splits string into a tuple of three strings - the stuff before str, str itself and all the stuff after str. string.rpartition(str) The same as partition(), but it searches from the end instead. Winter 2016

29 CISC101 - Prof. McLeod29 Tuple or List Returns, Cont. string.split(str=“ “, num=string.count(str)) Returns a list of strings parsed out of string using str as a delimiter. num can specify a maximum size to the list. If str is not supplied, a strip() is applied and then whitespace is used as a delimiter. string.splitlines(num=string.count(‘\n’)) Splits string and returns a list using the newline character as a delimiter. All newline characters are removed. Winter 2016


Download ppt "Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions."

Similar presentations


Ads by Google