String Processing CS 1111 Introduction to Programming Fall 2018

Similar documents
Strings. Upsorn Praphamontripong. Note: for reference when we practice loop. We ll discuss Strings in detail after Spring break

Working with Sequences: Section 8.1 and 8.2. Bonita Sharif

Chapter 8: More About Strings. COSC 1436, Summer 2018 Dr. Zhang 7/10/2018

Strings are actually 'objects' Strings

MULTIPLE CHOICE. Chapter Seven

Python Tutorial. Day 1

PYTHON MOCK TEST PYTHON MOCK TEST III

Chapter 10 Characters, Strings, and the string class

DECODE SPECIAL OPERATOR, FORMAT OPERATOR CONTENTS TRIPLE QUOTES. IS a-to-z METHODS REPLACE L E N G T H E X P A N D T A B S ENC0D3

COLLEGE OF ENGINEERING, NASHIK-4

UNIT-III. All expressions involving relational and logical operators will evaluate to either true or false

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Chapter 10: Characters, C- Strings, and More About the string Class

Chapter 10: Character Testing. From Program Character Case Conversion 8/23/2014. Character Testing. Character Case Conversion

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

STRINGS. We ve already introduced the string data type a few lectures ago. Strings are subtypes of the sequence data type.

Introduction to: Computers & Programming: Strings and Other Sequences

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Chapter 10: Characters, C- Strings, and More About the string Class Character Testing

Computing with Strings. Learning Outcomes. Python s String Type 9/23/2012

Module 3: Strings and Input/Output

Composite)Types. Types)Of)Variables. Addresses)And)References)(2) Addresses)And)References. Small)Example)Programs)Using)Strings

Introduction to String Manipulation

Babu Madhav Institute of Information Technology, UTU 2015

18.1. CS 102 Unit 18. Python. Mark Redekopp

Introductory Linux Course. Python I. Martin Dahlö UPPMAX. Author: Nina Fischer. Dept. for Cell and Molecular Biology, Uppsala University

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

Text. Text Actions. String Contains

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

Strings. Chapter 6. Python for Everybody

Introduction to Python for Plone developers

C mini reference. 5 Binary numbers 12

Handling Strings and Bytes

Princeton University COS 333: Advanced Programming Techniques A Subset of Python 2.7

IPSL python tutorial: some exercises for beginners

CS 115 Lecture 13. Strings. Neil Moore. Department of Computer Science University of Kentucky Lexington, Kentucky

Advanced Algorithms and Computational Models (module A)

Python Strings. Stéphane Vialette. LIGM, Université Paris-Est Marne-la-Vallée. September 25, 2012

Full file at

Variables, Constants, and Data Types

Introduction to Python

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

Advanced Python. Executive Summary, Session 1

CMSC201 Computer Science I for Majors

"Hello" " This " + "is String " + "concatenation"

UTORid: Lecture Section: (circle one): L0101 (MWF10) L0201 (MWF11) Instructor: Jacqueline Smith Jen Campbell

VENTURE. Section 1. Lexical Elements. 1.1 Identifiers. 1.2 Keywords. 1.3 Literals

Strings, Lists, and Sequences

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

Fundamentals of Programming. Lecture 11: C Characters and Strings

Announcements COMP 141. Accessing Characters Review. Using len function 10/30/2017. Strings II. Reminders: Program 6 - due 11/2

CMSC201 Computer Science I for Majors

Python - 2. Jim Eng

CSCE 110 PROGRAMMING FUNDAMENTALS

Scientific Programming in C V. Strings

Strings and Library Functions

Introductory Linux Course. Python I. Pavlin Mitev UPPMAX. Author: Nina Fischer Dept. for Cell and Molecular Biology, Uppsala University

System Design and Programming II

COMP519 Web Programming Lecture 17: Python (Part 1) Handouts

Chapter 8 - Characters and Strings

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Chapter 5 BET TER ARRAYS AND STRINGS HANDLING

6. Data Types and Dynamic Typing (Cont.)

Chapter 10: Creating and Modifying Text Lists Modules

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python

Structured programming

Topic 2. Big C++ by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

UNIVERSITÀ DI PADOVA. < 2014 March >

CPD for GCSE Computing: Practical Sheet 6 February 14

Chapter 8 C Characters and Strings

C: How to Program. Week /May/28

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Student Number: Comments are not required except where indicated, although they may help us mark your answers.

PYTHON- AN INNOVATION

LECTURE 02 INTRODUCTION TO C++

7. String Methods. Methods. Methods. Data + Functions Together. Designing count as a Function. Three String Methods 1/22/2016

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

today cs3157-fall2002-sklar-lect05 1

Student Number: Instructor: Brian Harrington

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

CMPT 125: Lecture 3 Data and Expressions

Zero Buffer Documentation

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Overview of List Syntax

COMP 364: Classes, Objects, and Names

Python Companion to Data Science

CS 115 Lecture 4. More Python; testing software. Neil Moore

Python. Executive Summary

Week 4. Strings, if/else, return, user input

Introduction to Computer Programming for Non-Majors

CSC102 INTRO TO PROGRAMMING WITH PYTHON LECTURE 9 PYTHON BEATS YOUR STINKY LANGUAGE: YOU CAN QUOTE ME MICHAEL GROSSBERG

Variable and Data Type I

Python for Informatics

Introduction to Bioinformatics

VLC : Language Reference Manual

7. String Methods. Topics: Methods and Data More on Strings Functions and Methods The String Class

Basic Computation. Chapter 2

Basic Computation. Chapter 2

Transcription:

String Processing CS 1111 Introduction to Programming Fall 2018 [The Coder s Apprentice, 10] 1

Collections Ordered, Dup allow List Range String Tuple Unordered, No Dup Dict collection[index] Access an element s value via index Index is int, starting at 0 collection[key] Access an element s value via key Key is primitive type, unique 2

Strings Review Sequence of characters (letters, numbers, punctuation marks, spaces, ) String literals = sequence of characters enclosed by quotations print( Hello World! ) Quotations inside quotations Python s fun! Must match quotations Single with single, double with double 3

Length of Strings Review Length of a string = the number of characters in a string len( Hello World! ) len( ) # length = 12 # length of empty string = 0 4

String Concatenation ( + ) Attach string to another string Review firstname = Thomas lastname = Jefferson name = firstname + + lastname print( Name is, name) # Name is Thomas Jefferson 5

String Repetition ( * ) Review Produce a string that is composed of repeated characters dashes = - * 10 # dashes = ---------- Note: results in string 6

String Conversion Review Convert numbers to strings average_grades = 85 result = Average Test1 grade is + str(average_grades) print(result) What happens if average_grades is not casted Convert strings to numbers prod_id = int( 149 ) price = float( 85.45 ) print(prod_id) print(price) 7

Special Characters Review Escape character print( Python is an \ interpreted\ language ) New line character print( Jake\nJohn\nJane\n ) 8

String Equality string1 equals to string2 if and only if They are of the same length, and They contain the exact same sequence of characters, and case sensitive name1 = Thomas Jefferson name2 = Thomas Jefferson name3 = Thomas jefferson name4 = Thomas Jenkinson # only name1 = name2 9

Slicing Refer to a range of values s[start : stop] Slice from start index (inclusive) to stop index (exclusive) s[start : ] Slice from start index (inclusive) to the end of the collection s[ : stop] Slice from the beginning of the collection to the stop index (exclusive) s[start : stop : step] Slice from start index (inclusive) to stop index (exclusive), skip step-1 then grab 10

substring in string in Operator Returns True if substring exists in string False, otherwise print( Jeff in name) # True 11

isdigit( ) String Testing Methods: isdigit( ), isspace( ) Returns True if the string s consists of only digits and contains at least one character; and False otherwise print(name.isdigit()) # False, contains non-digits isspace( ) Returns True if the string s consists of only white space characters (blank, newline, tab) and contains at least one character; and False otherwise print(name.isspace()) # False, contains non-white space 12

String Testing Methods: isalnum( ), isalpha( ) isalnum( ) Returns True if the string s consists of only letters or digits and contains at least one character; and False otherwise print(name.isalnum()) # False, contains space isalpha( ) Returns True if the string s consists of only letters and contains at least one character; and False otherwise print(name.isalpha()) # False, contains space 13

islower( ) String Testing Methods: islower( ), isupper( ) Returns True if the characters in the string are lowercase and the string contains at least one character; and False otherwise print(name.islower()) # False, contains uppercase isupper( ) Returns True if the characters in the string are uppercase and the string contains at least one character; and False otherwise print(name.isupper()) # False, contains lowercase 14

lower( ) String Modification Methods: lower( ), upper( ) Convert a string to lowercase print(name.lower()) # thomas jefferson upper( ) Convert a string to uppercase print(name.upper()) # THOMAS JEFFERSON 15

String Modification Methods: strip( ), rstrip( ), and lstrip( ) strip(char) Returns a copy of the string with all instances of char that appear at the beginning and the end of the string removed name = Thomas Jefferson print(name.strip()) rstrip(char) Returns a copy of the string with all instances of char that appear at the end of the string removed lstrip(char) Returns a copy of the string with all instances of char that appear at the beginning of the string removed Default: remove whitespace characters spaces, newlines (\n), and tabs (\t) # Thomas Jefferson (no spaces) 16

Other String Methods: join( ) join(seq_list) Returns a copy of the string, which is the concatenation of the string with intervening occurrences of seq_list. seq_list must be a list name = "Thomas Jefferson" seq_list = ["$", "--", "@"] print(name.join(seq_list)) # $Thomas Jefferson--Thomas Jefferson@ 17

Other String Methods: split( ) split(delimiter) Returns a list of all the words in the string, using delimiter as the separator Default separator: space print(name.split( )) # ['Thomas', 'Jefferson'] 18

Other String Methods: count( ) count(substring) Returns the number of non-overlapping occurrences of substring in the string s print(name.count( f )) print(name.count( ff )) # 2 # 1 19

Search and Replace Methods: startswith( ) startswith(substring) Returns True if the string s begins with substring and False otherwise print(name.startswith( Th )) # True 20

Search and Replace Methods: endswith( ) endswith(substring) Returns True if the string s ends with substring and False otherwise print(name.endswith( son )) # True 21

Search and Replace Methods: find( ) and rfind( ) find(substring) Returns the lowest index in the string s where substring begins, or -1 if substring is not found print(name.find( f )) print(name.find( cs1111 )) # 9 # -1 rfind(substring) Returns the highest index in the string s where substring begins, or -1 if substring is not found print(name.rfind( f )) # 10 22

Search and Replace Methods: index( ) index(substring, beg=0 end=len(string)) Returns the lowest index in the string s where substring begins, or raises an exception (ValueError: substring not found) if substring is not found str1 = "Thomas Jefferson" str2 = "e" print(str1.index(str2)) # 8 print(str1.index(str2, 9)) # 11 print(str1.index(str2, 9, 12)) # 11 print(str1.index(str2, 9, 11)) # error # from index 9, up to index 11 (not include index 11) 23

Search and Replace Methods: replace( ) replace(old, new) Returns a copy of the string with all instances of old replaced by new print(name.replace( Thomas, George )) # George Jefferson 24

Summary Must know (based on exam2 topic list, as of 10/15/2018) substring in string string1 + string2 string.lower() string.upper() string.strip() string.split() string.split(delimiter) string.find(substring) 25