Winter 2018 CISC101 11/16/2018 CISC101 Reminders

Slides:



Advertisements
Similar presentations
Manipulating Strings String Functions. VB provides a large number of functions that facilitate working with strings. These are found in Microsoft.VisualBasic.Strings.
Advertisements

Strings. Strings are amongst the most popular types in Python. We can create them simply by enclosing characters in quotes. Python treats single quotes.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
CATHERINE AND ANNIE Python: Part 4. Strings  Strings are interesting creatures. Although words are strings, anything contained within a set of quotes.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Manipulating Strings. What is a string? Data composed of text characters. The data is stored in consecutive bytes of memory. Each byte stores the ASCII.
Built-in Data Structures in Python An Introduction.
 2002 Prentice Hall. All rights reserved. 1 Chapter 13 – String Manipulation and Regular Expressions Outline 13.1 Introduction 13.2 Fundamentals of Characters.
EEL 3801C EEL 3801 Part I Computing Basics. EEL 3801C Data Representation Digital computers are binary in nature. They operate only on 0’s and 1’s. Everything.
Strings The Basics. Strings a collection data type can refer to a string variable as one variable or as many different components (characters) string.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Today… Style, Cont. – Naming Things! Methods and Functions Aside - Python Help System Punctuation Winter 2016CISC101 - Prof. McLeod1.
Today… Strings: –String Methods Demo. Raising Exceptions. os Module Winter 2016CISC101 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
LECTURE 5 Strings. STRINGS We’ve already introduced the string data type a few lectures ago. Strings are subtypes of the sequence data type. Strings are.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Strings in Python String Methods. String methods You do not have to include the string library to use these! Since strings are objects, you use the dot.
Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:
Do-more Technical Training
Excel STDEV.S Function.
3.1 Denary, Binary and Hexadecimal Number Systems
Containers and Lists CIS 40 – Introduction to Programming in Python
String Processing Upsorn Praphamontripong CS 1110
Strings Part 1 Taken from notes by Dr. Neil Moore
CISC101 Reminders Quiz 2 this week.
Basic Python Collective variables (sequences) Lists (arrays)
String Manipulation Part 2
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
CISC101 Reminders Slides have changed from those posted last night…
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Python - Strings.
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Winter 2018 CISC101 12/5/2018 CISC101 Reminders
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
CISC101 Reminders Quiz 2 this week.
Coding Concepts (Data- Types)
Winter 2019 CISC101 2/17/2019 CISC101 Reminders
CISC124 Labs start this week in JEFF 155. Fall 2018
functions: argument, return value
CS 1111 Introduction to Programming Spring 2019
CISC101 Reminders All assignments are now posted.
15-110: Principles of Computing
590 Scraping – NER shape features
CISC101 Reminders Assignment 2 due today.
Topics Basic String Operations String Slicing
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Introduction to Computer Science
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
CS2911 Week 2, Class 3 Today Return Lab 2 and Quiz 1
CISC101 Reminders All assignments are now posted.
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Topics Basic String Operations String Slicing
Winter 2019 CISC101 5/17/2019 CISC101 Reminders
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
CISC101 Reminders Assignment 3 due today.
Winter 2019 CISC101 5/30/2019 CISC101 Reminders
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Topics Basic String Operations String Slicing
STRING MANUPILATION.
Introduction to Computer Science
Presentation transcript:

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

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 karen@cs.queensu.ca Winter 2018 CISC101 - Prof. McLeod

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

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

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 (6553510, 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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, emails,… Python text manipulation code is very compact and can be quite powerful. Winter 2018 CISC101 - Prof. McLeod

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

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