Presentation is loading. Please wait.

Presentation is loading. Please wait.

Winter 2018 CISC101 11/16/2018 CISC101 Reminders

Similar presentations


Presentation on theme: "Winter 2018 CISC101 11/16/2018 CISC101 Reminders"— Presentation transcript:

1 Winter 2018 CISC101 11/16/2018 CISC101 Reminders Quiz 3 this week in lab. Topics in slides from last Tuesday. Turtle not on quiz. Sets not on quiz. Keyword and Default Arguments on quiz. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

2 Computing Undergraduate Information Night
If you are interested in going any further in Computing: Thursday, March 15 5:30 to 7:30pm Walter Light Hall, Rm. 210 Speakers from each program, Pizza and drinks! RSVP to Karen Knight at Winter 2018 CISC101 - Prof. McLeod

3 Today Continue Strings. Take a quick look at characters again.
Winter 2018 CISC101 - Prof. McLeod

4 Characters What strings are made of!
CISC101 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. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

5 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 ( , 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 2018 CISC101 - Prof. McLeod

6 Character Demo Programs
See UnicodeTable.py, and UnicodeBoxDemo.py. See Unicode.org for more information. Winter 2018 CISC101 - Prof. McLeod

7 string_variable.method_name()
String Methods 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() Winter 2018 CISC101 - Prof. McLeod

8 string.count(str, beg=0, end=len(string))
string.capitalize() string.center(width) string.count(str, beg=0, end=len(string)) string.endswith(str, 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 2018 CISC101 - Prof. McLeod

9 string.partition(str)
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 2018 CISC101 - Prof. McLeod

10 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 2018 CISC101 - Prof. McLeod

11 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 2018 CISC101 - Prof. McLeod

12 Boolean Returns string.endswith(str, beg=0, end=len(string)) Returns True if string has str at the end of the string or False otherwise. str 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(str, beg=0, end=len(string)) Just like endswith(), but looks at the start of string instead. Winter 2018 CISC101 - Prof. McLeod

13 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 2018 CISC101 - Prof. McLeod

14 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 2018 CISC101 - Prof. McLeod

15 string.count(str, beg=0, end=len(string))
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 2018 CISC101 - Prof. McLeod

16 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 2018 CISC101 - Prof. McLeod

17 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 2018 CISC101 - Prof. McLeod

18 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 2018 CISC101 - Prof. McLeod

19 string.expandtabs(tabsize=8)
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 2018 CISC101 - Prof. McLeod

20 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 2018 CISC101 - Prof. McLeod

21 string.replace(str1, str2, num=string.count(str1))
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 2018 CISC101 - Prof. McLeod

22 string.translate(table)
String Returns, Cont. string.zfill(width) Returns a version of string, usually a number, with a padding of zeros to give a string of size width. If a minus sign is present it stays to the left of the zeros. string.translate(table) Returns a string where the characters in string have been mapped to other characters according to the mapping provided in table. Winter 2018 CISC101 - Prof. McLeod

23 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. If str is not found, then string followed by two empty strings is returned. string.rpartition(str) The same as partition(), but it searches from the end instead. Winter 2018 CISC101 - Prof. McLeod

24 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 2018 CISC101 - Prof. McLeod

25 Demo of String Methods Starts with the tuple or list returns methods, then looks at how you could analyze larger amounts of text. See StringMethodsDemo.py This demo did not work with a very large amount of text, but the same techniques could easily be applied to much larger documents – newspapers, transcribed speeches, books, texts, s,… Python text manipulation code is very compact and can be quite powerful. Winter 2018 CISC101 - Prof. McLeod

26 Could You Write a String Method?
Knowing only: ASCII character codes (or how to get them). How to loop through a string String operators And the len(), ord() and chr() BIFS Could you write all of the string methods yourself? Sure!! Winter 2018 CISC101 - Prof. McLeod

27 You Can Write These! But, since we don’t know how to extend the string class in order to add methods to it, let’s just write a function instead. Imitate something like .swapcase(), for example: Write a function called upperLower (?) that changes upper case letters to lower case and lower case letters to upper case. See TestUpperLower.py Winter 2018 CISC101 - Prof. McLeod


Download ppt "Winter 2018 CISC101 11/16/2018 CISC101 Reminders"

Similar presentations


Ads by Google