Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

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

Chapter 4 Working with Strings. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Sequence of characters.
Introduction to Computing Using Python Chapter 6  Encoding of String Characters  Randomness and Random Sampling.
Last Updated March 2006 Slide 1 Regular Expressions.
Introduction to Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Introduction to Python
Chapter 6 Functions -- QuickStart. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a function?
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Examples of comparing strings. “ABC” = “ABC”? yes “ABC” = “ ABC”? No! note the space up front “ABC” = “abc” ? No! Totally different letters “ABC” = “ABCD”?
Chapter 4 Working with Strings. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Sequence of characters.
Exam Prep and Wrap-Up Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Advanced Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Thinking about programming Intro to Computer Science CS1510 Dr. Sarah Diesburg.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
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.
Lesson 4 String Manipulation. Lesson 4 In many applications you will need to do some kind of manipulation or parsing of strings, whether you are Attempting.
String and Lists Dr. José M. Reyes Álamo.
Thinking about programming
CSc 120 Introduction to Computer Programing II Adapted from slides by
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Lecture 19 Strings and Regular Expressions
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Thinking about programming
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Strings Part 1 Taken from notes by Dr. Neil Moore
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Strings
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lists – Indexing and Sorting
Types, Truth, and Expressions (Part 2)
String Encodings and Penny Math
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
String Manipulation Chapter 7 Attaway MATLAB 4E.
T. Jumana Abu Shmais – AOU - Riyadh
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
String and Lists Dr. José M. Reyes Álamo.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Coding Concepts (Data- Types)
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lists – Indexing and Sorting
Types, Truth, and Expressions (Part 2)
CS190/295 Programming in Python for Life Sciences: Lecture 3
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
15-110: Principles of Computing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Computer Science
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Introduction to Strings
String Encodings and Penny Math
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Types, Truth, and Expressions (Part 2)
Presentation transcript:

Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg Advanced Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Review of the base operators for strings myStr[3] myStr[3:6] Addition Multiplication in

Another Operator Can check to see if a substring exists in the string using the in operator. Returns True or False myStr = ‘aabbccdd’ ‘a’ in myStr  True ‘abb’ in myStr  True ‘x’ in myStr  False

String Methods and Functions

Functions What is a function? Name several we have worked with.

Functions, First Cut A function is a program that performs some operation(s). Its details are hidden (encapsulated), only its interface provided. A function takes some number of inputs (arguments) and returns a value based on the arguments and the function’s operation.

String Method A method is a variation on a function like a function, it represents a program like a function, it has input arguments and an output Unlike a function, it is applied in the context of a particular object. This is indicated by the ‘dot notation’ invocation

Example upper is the name of a method. It generates a new string that has all upper case characters of the string it was called with. myStr = ‘Python Rules!’ myStr.upper()  ‘PYTHON RULES!’ The string myStr called the upper() method, indicated by the dot between them.

More Dot Notation In generation, dot notation looks like: object.method(…) It means that the object in front of the dot is calling a method that is associated with that object’s type. The methods that can be called are tied to the type of the object calling it. Each type has different methods.

Find myStr = ‘hello’ myStr.find(‘l’) # find index of ‘l’ in myStr  2 Note how the method ‘find’ operates on the string object myStr and the two are associated by using the “dot” notation: myStr.find(‘l’). Terminology: the thing(s) in parenthesis, i.e. the ‘l’ in this case, is called an argument.

Chaining Methods Methods can be chained together. Perform first operation, yielding an object Use the yielded object for the next method myStr = ‘Python Rules!’ myStr.upper()  ‘PYTHON RULES!’ myStr.upper().find(‘O’)  4

Optional Arguments Some methods have optional arguments: if the user doesn’t provide one of these, a default is assumed find has a default second argument of 0, where the search begins aStr = ‘He had the bat’ aStr.find(‘t’)  7 # 1st ‘t’,start @ 0 aStr.find(‘t’,8)  13 # 2nd ‘t’

Nesting Methods You can “nest” methods, that is, the result of one method as an argument to another. Remember that parenthetical expressions are done “inside out”: do the inner parenthetical expression first, then the next, using the result as an argument. aStr.find(‘t’, aStr.find(‘t’)+1) Translation: find the second ‘t’.

How to Know? You can use IDLE to find available methods for any type. You enter a variable of the type, followed by the ‘.’ (dot) and then a tab. Remember, methods match with a type. Different types have different methods. If you type a method name, IDLE will remind you of the needed and optional arguments.

More Methods (Even more exist: http://docs. python s.capitalize s.center(width) s.count(sub,[,start [,end]]) s.ljust(width) s.lower() s.upper() s.lstrip() s.rfind(sub, [,start [,end]]) s.splitlines([keepends]) s.strip() s.translate(table [, delchars])

String Comparisons, Single Char There are two systems for representing characters: ASCII and Unicode ASCII takes the English letters, numbers and punctuation marks and associates them with an integer number Single character comparisons are based on that number

Comparisons Within Sequence It makes sense to compare within a sequence (lower case, upper case, digits). ‘a’ < ‘b’ True ‘A’ < ‘B’ True ‘1’ < ‘9’ True Can be weird outside of the sequence: ‘a’ < ‘A’ False ‘a’ < ‘0’ False

Whole Strings Compare the first element of each string: if they are equal, move on to the next character in each if they are not equal, the relationship between those to characters are the relationship between the string if one ends up being shorter (but equal), the shorter is smaller

Examples ‘a’ < ‘b’ True ‘aaab’ < ‘aaac’ ‘aa’ < ‘aaz’ First difference is at the last char. ‘b’<‘c’ so ‘aaab’ is less than ‘aaac’. True. ‘aa’ < ‘aaz’ The first string is the same but shorter. Thus it is “smaller”. True.

String Formatting I’m probably not going to lecture on this but you should read through this material