Chapter 8 More on Strings and Special Methods

Slides:



Advertisements
Similar presentations
Container Types in Python
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.
Strings in Java 1. strings in java are handled by two classes String &
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.
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.
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 8 More on Strings and Special Methods 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.
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.
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.
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.
More Strings CS303E: Elements of Computers and Programming.
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.
Mr. Fowler Computer Science 14 Feb 2011 Strings in Python.
1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
Python - 2 Jim Eng Overview Lists Dictionaries Try... except Methods and Functions Classes and Objects Midterm Review.
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.
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.
Python Strings. String  A String is a sequence of characters  Access characters one at a time with a bracket operator and an offset index >>> fruit.
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.
Chapter 4 Computing With Strings Charles Severance Textbook: Python Programming: An Introduction to Computer Science, John Zelle.
String Manipulation Part 1
String Methods Programming Guides.
Strings Chapter 6 Python for Everybody
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
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
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Introduction to Strings
Lists in Python.
Chapter 8 More on Strings and Special Methods
Python - Strings.
10.1 Character Testing.
Chapter 8 More on Strings and Special Methods
CSC 221: Introduction to Programming Fall 2018
Python for Informatics: Exploring Information
CEV208 Computer Programming
Topics Sequences Introduction to Lists List Slicing
Basic String Operations
Introduction to Strings
CS 1111 Introduction to Programming Spring 2019
Topics Basic String Operations String Slicing
Introduction to Computer Science
Topics Sequences Introduction to Lists List Slicing
CSE 231 Lab 3.
IST256 : Applications Programming for Information Systems
Sequences Sequences are collections of data values that are ordered by position A string is a sequence of characters A list is a sequence of any Python.
Topics Basic String Operations String Slicing
By Himanshi dixit 11th ’B’
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
CMPT 120 Topic: Python strings.
Topics Basic String Operations String Slicing
STRING MANUPILATION.
Presentation transcript:

Chapter 8 More on Strings and Special Methods

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

Objectives 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). To strip whitespaces from the left and/or right of a string by using the methods lstrip, rstrip, and strip (§8.2.12). To format strings by using the methods center, ljust, rjust, and format (§8.2.13). To apply strings in the development of applications (CheckPalindrome, HexToDecimalConversion) (§§8.3-8.4). To define special methods for operators (§8.5). To design the Rational class for representing rational numbers (§8.6).

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

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.

Functions for str >>> s = "Welcome" >>> len(s) 7 >>> max(s) o >>> min(s) W

Index Operator []

The Slicing Operator [start : end]

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

Negative Index >>> s1 = "Welcome" >>> s1[-1] #Same as s1[len(s1)- 1] = s1[6] ‘e’ >>> s1[-3 : -1] # Same as s1[4 : 6] ‘om’ If index (i or j) in the slice operation s[i : j] is negative, replace the index with len(s) + index.

The in and not in Operators >>> s1 = "Welcome" >>> "come" in s1 True >>> "come" not in s1 False >>>

Comparing Strings >>> s1 = "green" >>> s2 = "glow" False >>> s1 != s2 True >>> s1 > s2 >>> s1 >= s2 >>> s1 < s2 >>> s1 <= s2

for ch in string: print(ch) For each Loops String = “Welcome" for ch in string: print(ch) for i in range(0, len(s), 2): print(s[i]) Output: W e l c o m Output: W l o e

Testing Characters in a String

isidentifier() checks if the string could be a valid identifier

Searching for Substrings

Converting Strings

Notice: The original string is NEVER changed

Stripping Whitespace Characters

Formatting Strings

Problem: Finding Palindromes Objective: Checking whether a string is a palindrome: a string that reads the same forward and backward. CheckPalindrome Run

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.

Example: Adding Two Points and Printing a Example: Adding Two Points and Printing a Point using Operator Overloading class Point: def __init__(self, x = 0, y = 0): self.x = x self.y = y from Point import Point p1 = Point(2,3) p2 = Point(-1,2) print(p1 + p2) print(p1 + p2) TypeError: unsupported operand type(s) for +: 'Point' and 'Point'

Point.py class Point: def __init__(self, x = 0, y = 0): self.x = x self.y = y def __add__(self, other): x = self.x + other.x y = self.y + other.y return Point(x, y) def __str__(self): return "({0}, {1})".format(self.x, self.y) TestPoint.py from Point import Point p1 = Point(2 ,3) p2 = Point(-1, 2) print(p1 + p2) (1,5) >>> TestPoint Run

Operators and Methods

The Rational Class Rational TestRationalClass Run

Exercise 8.17