Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Advertisements

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.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Part 1 Conditionals and Loops.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
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:
Chapter 5 new The Do…Loop Statement
Fundamentals of Python: First Programs
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 4 Decision Structures and Boolean Logic.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 8 More on Strings and Special Methods 1.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Fundamentals of Python: First Programs
Built-in Data Structures in Python An Introduction.
CIT 590 Intro to Programming Lecture 5 – completing lists.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Lecture 19 - More on Lists, Slicing Lists, List Functions COMPSCI 101 Principles of Programming.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Chapter 7: Characters, Strings, and the StringBuilder.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 Dictionaries and Sets.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter Arrays, Timers, and More 8.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
INLS 560 – S TRINGS Instructor: Jason Carter. T YPES int list string.
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.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
© 2010 Lawrenceville Press Slide 1 Chapter 5 The Do…Loop Statement  Loop structure that executes a set of statements as long as a condition is true. 
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.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
String and Lists Dr. José M. Reyes Álamo.
String Methods Programming Guides.
String Processing Upsorn Praphamontripong CS 1110
Primitive Types Vs. Reference Types, Strings, Enumerations
Introduction to Strings
Introduction to Strings
Chapter 7: Strings and Characters
Bryan Burlingame Halloween 2018
Chapter 8 JavaScript: Control Statements, Part 2
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Topics Sequences Introduction to Lists List Slicing
Basic String Operations
Introduction to Strings
CS 1111 Introduction to Programming Spring 2019
Topics Sequences Lists Copying Lists Processing Lists
The Data Element.
Topics Basic String Operations String Slicing
Introduction to Computer Science
Bryan Burlingame Halloween 2018
Topics Sequences Introduction to Lists List Slicing
Introduction to Strings
Topics Basic String Operations String Slicing
The Data Element.
Chapter 8 JavaScript: Control Statements, Part 2
Topics Basic String Operations String Slicing
Presentation transcript:

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Topics Basic String Operations String Slicing Testing, Searching, and Manipulating Strings

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Basic String Operations Many types of programs perform operations on strings In Python, many tools for examining and manipulating strings –Strings are sequences, so many of the tools that work with sequences work with strings

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Accessing the Individual Character in a String To access an individual character in a string: –Use a for loop Format: for character in string: Useful when need to iterate over the whole string, such as to count the occurrences of a specific character –Use indexing Each character has an index specifying its position in the string, starting at 0 Format: character = my_string[i]

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Accessing the Individual Character in a String (cont’d.)

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Accessing the Individual Character in a String (cont’d.) IndexError exception will occur if: –You try to use an index that is out of range for the string –Likely to happen when loop iterates beyond the end of the string len(string) function can be used to obtain the length of a string –Useful to prevent loops from iterating beyond the end of a string

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley String Concatenation Concatenation: appending one string to the end of another string –Use the + operator to produce a string that is a combination of its operands –The augmented assignment operator += can also be used to concatenate strings The operand on the left side of the += operator must be an existing variable; otherwise, an exception is raised

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Strings Are Immutable Strings are immutable –Once they are created, they cannot be changed Concatenation doesn’t actually change the existing string, but rather creates a new string and assigns the new string to the previously used variable –Cannot use an expression of the form string[index] = new_character Statement of this type will raise an exception

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Strings Are Immutable (cont’d.)

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley String Slicing Slice: span of items taken from a sequence, known as substring –Slicing format: string[start : end] Expression will return a string containing a copy of the characters from start up to, but not including, end If start not specified, 0 is used for start index If end not specified, len(string) is used for end index –Slicing expressions can include a step value and negative indexes relative to end of string

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Testing, Searching, and Manipulating Strings You can use the in operator to determine whether one string is contained in another string –General format: string1 in string2 string1 and string2 can be string literals or variables referencing strings Similarly you can use the not in operator to determine whether one string is not contained in another string

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley String Methods Strings in Python have many types of methods, divided into different types of operations –General format: mystring.method(arguments) Some methods test a string for specific characteristics –Generally Boolean methods, that return True if a condition exists, and False otherwise

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley String Methods (cont’d.)

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley String Methods (cont’d.) Some methods return a copy of the string, to which modifications have been made –Simulate strings as mutable objects String comparisons are case-sensitive –Uppercase characters are distinguished from lowercase characters – lower and upper methods can be used for making case-insensitive string comparisons

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley String Methods (cont’d.)

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley String Methods (cont’d.) Programs commonly need to search for substrings Several methods to accomplish this: – endswith(substring) : checks if the string ends with substring Returns True or False – startswith(substring) : checks if the string starts with substring Returns True or False

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley String Methods (cont’d.) Several methods to accomplish this (cont’d): – find(substring) : searches for substring within the string Returns lowest index of the substring, or if the substring is not contained in the string, returns -1 – replace(substring, new_string) : Returns a copy of the string where every occurrence of substring is replaced with new_string

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley String Methods (cont’d.)

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Repetition Operator Repetition operator: makes multiple copies of a string and joins them together –The * symbol is a repetition operator when applied to a string and an integer String is left operand; number is right –General format: string_to_copy * n –Variable references a new string which contains multiple copies of the original string

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Splitting a String split method: returns a list containing the words in the string –By default, uses space as separator –Can specify a different separator by passing it as an argument to the split method

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Summary This chapter covered: –String operations, including: Methods for iterating over strings Repetition and concatenation operators Strings as immutable objects Slicing strings and testing strings String methods Splitting a string