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

Slides:



Advertisements
Similar presentations
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Advertisements

UNIX Filters.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
AOIT Introduction to Programming Unit 3, Lesson 10 Advanced Sequence Manipulation Copyright © 2009–2012 National Academy Foundation. All rights reserved.
STRINGS CMSC 201 – Lab 3. Overview Objectives for today's lab:  Obtain experience using strings in Python, including looping over characters in strings.
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
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.
Built-in Data Structures in Python An Introduction.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Chapter 5 Strings CSC1310 Fall Strings Stringordered storesrepresents String is an ordered collection of characters that stores and represents text-based.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 3 Mathematical Functions, Strings, and Objects.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
More Strings CS303E: Elements of Computers and Programming.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Strings The Basics. Strings a collection data type can refer to a string variable as one variable or as many different components (characters) string.
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
Python Files and Lists. Files  Chapter 9 actually introduces you to opening up files for reading  Chapter 14 has more on file I/O  Python can read.
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.
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:
String and Lists Dr. José M. Reyes Álamo.
String Methods Programming Guides.
Containers and Lists CIS 40 – Introduction to Programming in Python
String Processing Upsorn Praphamontripong CS 1110
CISC101 Reminders Quiz 2 this week.
Winter 2018 CISC101 11/16/2018 CISC101 Reminders
Basic Python Collective variables (sequences) Lists (arrays)
String Manipulation Part 2
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
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.
String and Lists Dr. José M. Reyes Álamo.
Fall 2018 CISC124 2/15/2019 CISC124 TA names and s will be added to the course web site by the end of the week. Labs start next week in JEFF 155:
CS 1111 Introduction to Programming Spring 2019
CISC101 Reminders All assignments are now posted.
15-110: Principles of Computing
The Data Element.
CISC101 Reminders Assignment 2 due today.
Topics Basic String Operations String Slicing
Introduction to Computer Science
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
CISC101 Reminders All assignments are now posted.
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Python Strings.
Topics Basic String Operations String Slicing
The Data Element.
Winter 2019 CISC101 5/17/2019 CISC101 Reminders
CISC101 Reminders Assignment 3 due today.
Topics Basic String Operations String Slicing
STRING MANUPILATION.
Introduction to Computer Science
Presentation transcript:

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

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

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 s 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

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

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

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

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 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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