© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 8 More on Strings and Special Methods 1.

Slides:



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

Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
Ch 8. Characters and Strings Timothy Budd 2 Characters and Literals Strings Char in C++ is normally an 8-bit quantity, whereas in Java it is a 16-bit.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Java Programming Strings Chapter 7.
1 Python Chapter 3 Reading strings and printing. © Samuel Marateck.
Computer Science 111 Fundamentals of Programming I Sequences: Strings.
Strings. Strings are amongst the most popular types in Python. We can create them simply by enclosing characters in quotes. Python treats single quotes.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 14 Tuples, Sets, and Dictionaries 1.
Python for Informatics: Exploring Information
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 9 Strings and Text.
String & Composition. Agenda This keyword. String class. String operations. Composition.
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
Strings CS303E: Elements of Computers and Programming.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 8 Strings 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
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.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
String String Builder. System.String string is the alias for System.String A string is an object of class string in the System namespace representing.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 10: Characters, C- Strings, and More About.
File I/O CMSC 201. Overview Today we’ll be going over: String methods File I/O.
Notes on Python Regular Expressions and parser generators (by D. Parson) These are the Python supplements to the author’s slides for Chapter 1 and Section.
1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
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 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 10 Lists 1.
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.
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.
Unit 4 – Chapter 4 Strings and Tuples. len() Function You can pass any sequence you want to the len() function and it will return the length of the sequence.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings Chapter.
String Manipulation Part 1
CSC 131: Introduction to Computer Science
Notes on Python Regular Expressions and parser generators (by D
Module 4 String and list – String traversal and comparison with examples, List operation with example, Tuples and Dictionaries-Operations and examples.
String Processing Upsorn Praphamontripong CS 1110
Operator Overloading; Class string
CMPT 120 Topic: Python strings.
String String Builder.
Strings Part 1 Taken from notes by Dr. Neil Moore
Strings Chapter 6 Slightly modified by Recep Kaya Göktaş in April Python for Informatics: Exploring Information
Introduction to Strings
Introduction to Strings
Chapter 8 More on Strings and Special Methods
Chapter 8 More on Strings and Special Methods
Python - Strings.
10.1 Character Testing.
Chapter 8 More on Strings and Special Methods
Python for Informatics: Exploring Information
CEV208 Computer Programming
Basic String Operations
Introduction to Strings
CS 1111 Introduction to Programming Spring 2019
Topics Basic String Operations String Slicing
Introduction to Computer Science
CSE 231 Lab 3.
Introduction to Strings
IST256 : Applications Programming for Information Systems
Topics Basic String Operations String Slicing
CMPT 120 Topic: Python strings.
Topics Basic String Operations String Slicing
STRING MANUPILATION.
Unit-2 Objects and Classes
Presentation transcript:

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 8 More on Strings and Special Methods 1

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Objectives To learn how to create strings (§8.2.1). To use the len, min, and max functions to obtain the length of a string or the smallest or largest element in a string (§8.2.2). To access string elements by using the index operator ([])(§8.2.3). To get a substring from a larger string by using the slicing str[start:end] operator (§8.2.4). To concatenate strings by using the + operator and to duplicate strings by using the * operator (§8.2.5). To use the in and not in operators to determine whether a string is contained within another string (§8.2.6). To compare strings by using comparison operators (==, !=,, and >=) (§8.2.7). To iterate characters in a string by using a foreach loop (§8.2.8). To test strings by using the methods isalnum, isalpha, isdigit, isidentifier, islower, isupper, and isspace (§8.2.9). To search for substrings by using the methods endswith, startswith, find, rfind, and count (§8.2.10). To convert strings by using the methods capitalize, lower, upper, title, swapcase, and replace (§8.2.11). 2

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. The str Class Creating Strings s1 = str() # Create an empty string s2 = str("Welcome") # Create a string Welcome Python provides a simple syntax for creating string using a string literal. For example, s1 = "" # Same as s1 = str() s2 = "Welcome" # Same as s2 = str("Welcome") 3

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Strings are Immutable A string object is immutable. Once it is created, its contents cannot be changed. To optimize performance, Python uses one object for strings with the same contents. As shown in Figure 6.8, both s1 and s2 refer to the same string object. 4

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Functions for str >>> s = "Welcome" >>> len(s) 7 >>> max(s) o >>> min(s) W 5

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Index Operator [] 6

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. The +, *, [ : ], and in Operators >>> s1 = "Welcome" >>> s2 = "Python" >>> s3 = s1 + " to " + s2 >>> s3 ’Welcome to Python’ >>> s4 = 2 * s1 >>> s4 ’WelcomeWelcome’ >>> s1[3 : 6] ’com’ >>> 'W' in s1 True >>> 'X' in s1 False 7

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Negative Index >>> s1 = "Welcome" >>> s1[-1] ‘e’ >>> s1[-3 : -1] ‘me’ 8

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. The in and not in Operators >>> s1 = "Welcome" >>> "come" in s1 True >>> "come" not in s1 False >>> 9

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Foreach Loops 10 for ch in string: print(ch) for i in range(0, len(s), 2): print(s[i])

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Comparing Strings 11 >>> s1 = "green" >>> s2 = "glow" >>> s1 == s2 False >>> s1 != s2 True >>> s1 > s2 True >>> s1 >= s2 True >>> s1 < s2 False >>> s1 <= s2 False

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Testing Characters in a String 12

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Searching for Substrings 13

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Converting Strings 14

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Stripping Whitespace Characters 15

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Formatting Strings 16

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Problem: Finding Palindromes Objective: Checking whether a string is a palindrome: a string that reads the same forward and backward. 17 CheckPalindrome Run

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Problem: Converting Hex to Decimal 18 HexToDecimalConversion Run

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Operator Overloading Defining methods for operators is called operator overloading. Operator overloading allows the programmer to use the built-in operators for user-defined methods. These methods are named in a special way for Python to recognize the association. 19

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Operators and Methods 20

© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 21 The Rational Class 21 RationalRunTestRationalClass