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

Similar documents
Working with Sequences: Section 8.1 and 8.2. Bonita Sharif

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

String Processing CS 1111 Introduction to Programming Fall 2018

Strings are actually 'objects' Strings

Introduction to String Manipulation

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

MULTIPLE CHOICE. Chapter Seven

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

PYTHON MOCK TEST PYTHON MOCK TEST III

IPSL python tutorial: some exercises for beginners

Chapter 10 Characters, Strings, and the string class

COLLEGE OF ENGINEERING, NASHIK-4

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

Advanced Python. Executive Summary, Session 1

Python Tutorial. Day 1

Introduction to: Computers & Programming: Strings and Other Sequences

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

Strings. Chapter 6. Python for Everybody

Module 3: Strings and Input/Output

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

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

UNIVERSITÀ DI PADOVA. < 2014 March >

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

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

Student Number: Instructor: Brian Harrington

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

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

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

CMSC201 Computer Science I for Majors

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

Introduction to Python for Plone developers

Python. Executive Summary

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

6. Data Types and Dynamic Typing (Cont.)

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

CSCE 110 PROGRAMMING FUNDAMENTALS

Zero Buffer Documentation

CPD for GCSE Computing: Practical Sheet 6 February 14

CMSC201 Computer Science I for Majors

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

CS 102 Lab 3 Fall 2012

Strings. Looping. dna = 'ATGTAGC' print(dna) In [1]: ATGTAGC. You can loop over the characters of a string using a for loop, as we saw on Tuesday:

18.1. CS 102 Unit 18. Python. Mark Redekopp

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

Chapter 6: Files and Exceptions. COSC 1436, Summer 2016 Dr. Ling Zhang 06/23/2016

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

Python for Informatics

#5: Strings, Lists Reading: Chapter 4 and 5

Program Planning, Data Comparisons, Strings

Strings and Library Functions

Chapter 8 - Characters and Strings

COMP1730/COMP6730 Programming for Scientists. Strings

Scientific Programming in C V. Strings

They grow as needed, and may be made to shrink. Officially, a Perl array is a variable whose value is a list.

Variable and Data Type I

Python Companion to Data Science

C: How to Program. Week /May/28

Python - 2. Jim Eng

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

Python. Olmo Zavala R. Python Exercises. Center of Atmospheric Sciences, UNAM. August 24, 2016

Honors Computer Science Python Mr. Clausen Programs 4A, 4B, 4C, 4D, 4E, 4F

Intro to Strings. CSE 231 Rich Enbody. String: a sequence of characters. Indicated with quotes: or " " 9/11/13

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

The Practice of Computing Using PYTHON. Chapter 4. Working with Strings. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Introduction to Computer Programming for Non-Majors

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

Variable and Data Type I

Python. Chapter 3. Strings

Structured programming

Strings. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

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

CSc 120 Introduction to Computer Programing II Adapted from slides by Dr. Saumya Debray

For strings (and tuples, when we get to them), its easiest to think of them like primitives directly stored in the variable table.

STUDENT LESSON A10 The String Class

System Design and Programming II

Composite Types. You will learn how to create new variables that are collections of other entities. Types Of Variables.

Chapter 10: Creating and Modifying Text Lists Modules

Computing with Numbers

SPRING COMP 141 MIDTERM 2 PRACTICE PROBLEMS

Exceptions and File I/O

Introductory Linux Course. Python I. Nina Fischer Dept. for Cell and Molecular Biology, Uppsala University

Introductory Linux Course. Python I. Nina Fischer Dept. for Cell and Molecular Biology, Uppsala University

The second statement selects character number 1 from and assigns it to.

COMP 364: Classes, Objects, and Names

CS107 Spring 2019, Lecture 4 C Strings

Babu Madhav Institute of Information Technology, UTU 2015

UNIT 5. String Functions and Random Numbers

Chapter 6: Files and Exceptions. COSC 1436, Spring 2017 Hong Sun 3/6/2017

Strings. Genome 373 Genomic Informatics Elhanan Borenstein

C mini reference. 5 Binary numbers 12

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

Handling Strings and Bytes

Python. Karin Lagesen.

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

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

Fundamentals of Programming. Lecture 11: C Characters and Strings

String Objects: The string class library

MEIN 50010: Python Strings

Lists in Python CS 8: Introduction to Computer Science, Winter 2018 Lecture #10

Transcription:

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

Creating Strings The str Class 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") 2

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

Index Operator [] 0 1 2 3 4 5 6 7 8 9 10 s P r o g r a m m i n g s[0] s[1] s[10]

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] e >>> s1[-3 : -1] om

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

More about strings Accessing the individual characters in a string for variable in string: statement statement etc Example: name = Juliet for ch in name: print(ch)

Foreach loop for ch in string: print(ch) for i in range(0, len(s), 2): print(s[i])

More about strings https://www.tutorialspoint.com/python/python_strings.htm Accessing values in a string var1 = 'Hello World!' var2 = "Python Programming" print ("var1[0]: ", var1[0] ) print ("var2[1:5]: ", var2[1:5]) When the above code is executed, it produces the following result var1[0]: H var2[1:5]: ytho

String operations-cont Indexing and IndexError Exceptions Each character in a string has an index that specifies its position in the string. An IndexError exception will occur if you try to use an index that is out of range for particular string. Example: city = Boston Index = 0 While index < 7 print(city[index]) index +=1

String operation - conti len function Get length of a string: length = len(city) city = Boston Index = 0 While index < len(city) print(city[index]) index +=1 String Concatenation Using + to concatenate firstname = Emily lastname= Yeager fullname = firstname + lastname pring(fullname) Output: Emily Yeager Strings Are Immutable

String operations-cont String Slicing to get a slice of a string String[start : end] Example: fullname= Emily Yeager firstname=fullname[0: 5] print(firstname) output: Emily

String Special Operators Assume string variable a holds 'Hello' and variable b holds 'Python', then Operator Description Example + Concatenation - Adds values on either side of the operator * Repetition - Creates new strings, concatenating multiple copies of the same string [] Slice - Gives the character from the given index [ : ] Range Slice - Gives the characters from the given range a + b will give HelloPython a*2 will give -HelloHello a[1] will give e a[1:4] will give ell in not in Membership - Returns true if a character exists in the given string Membership - Returns true if a character does not exist in the given string H in a will give 1 M not in a will give 1

Testing, Searching, and Manipulating Strings Testing Strings with in and not in String1 in string2 String1 not in string2 Example: Names = Bill Joanne Susane Chris Juan Katie if Pierre not in Names: print( Pierre was not found ) else print( Pierre is found )

Testing, Searching, and Manipulating Strings Conti- Method isalnum() isalpha() isdigit() islower() isspace() isupper() Description Returns true if the string contains only alphabetic letters or digits and is at least one character in length. Returns false otherwise. Returns true if the string contains only alphabetic letters and is at least one character in length. Returns false otherwise. Returns true if the string contains only numeric digits and is at least one character in length. Returns false otherwise. Returns true if all of the alphabetic letters in the string are lowercase, and the string contains at least one alphabetic letter. Returns false otherwise. Returns true if the string contains only whitespace characters and is at least one character in length. Returns false otherwise. (Whitespace characters are spaces, newlines (\n), and tabs(\t). Returns true if all of the alphabetic letters in the string are uppercase, and the string contains at least one alphabetic letter. Returns false otherwise.

Testing, Searching, and Manipulating Strings Contin- String Modification Methods p354 lower() rstrip(char) lstrip() strip() lstrip(character) strip(char) rstrip() upper() Search and replace methods (p355) endswith(substring) find(substring) replace(old,new) startswith(substring) Returns true if the string ends with substring Returns lowest index in the string where substring is found. If not found, returns -1 Returns a coopy of the string with all insances of old replaced by new Returns true if the string starts with substring

Splitting a string http://www.jquery-az.com/python-split-string-methods-split-rsplit-and-splitlines-6- examples/ The split method returns a list containing the words in the string. Code example My_string = one two three four Word_list = () Print(Word_list) My_string.split Output: [ one, two, three, four ]

Lab and assignment Project discussion Lab: Chapter 8: exercise #1,#11 Review Question Assignment: Chapter 8: exercise #12 Due date: 7/12/2017