Fundamentals of Python: First Programs. Chapter 4: Strings (Indexing, Slicing, and Methods)

Similar documents
Fundamentals of Python: First Programs. Chapter 4: Text Files

Fundamentals of Python: First Programs. Chapter 4: Strings and Text Files

Python Class-Lesson1 Instructor: Yao

Slicing. Open pizza_slicer.py

Lesson 4: Type Conversion, Mutability, Sequence Indexing. Fundamentals of Text Processing for Linguists Na-Rae Han

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

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

142

Lesson 5: Introduction to Content Matchers

Programming Fundamentals and Python

Basic data types. Building blocks of computation

October 6, SET-UP (Activate Prior Knowledge & Connect to Challenge Question)

Homework 6: Spatial Games Due: 11:59pm on Friday, October 30

ENGR 102 Engineering Lab I - Computation

Lesson 15. Student Outcomes. Lesson Notes. Classwork. Opening (2 minutes) Opening Exercise (3 minutes) (optional)

More non-primitive types Lesson 06

Functions Defining Calling (invoking) Parameters Returned values

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

Sequences and Loops. Indices: accessing characters in a string. Old friend: isvowel. Motivation: How to count the number of vowels in a word?

An infinite decimal is a decimal with digits that do not end. They may repeat, but they never end. An example of an infinite decimal is..

Data Structures. Lists, Tuples, Sets, Dictionaries

Petros: A Multi-purpose Text File Manipulation Language

Level 3 Computing Year 2 Lecturer: Phil Smith

Microsoft Office Excel Use Excel s functions. Tutorial 2 Working With Formulas and Functions

Collections. Lists, Tuples, Sets, Dictionaries

CSE 331 Midterm Exam 2/13/12

[CHAPTER] 1 INTRODUCTION 1

3 ADT Implementation in Java

Lesson 20: Every Line is a Graph of a Linear Equation

Project #1 rev 2 Computer Science 2334 Fall 2013 This project is individual work. Each student must complete this assignment independently.

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Lesson 10. Student Outcomes. Lesson Notes

CE151-4-AU UNIVERSITY OF ESSEX. Sample Examination Questions 2011 INTRODUCTION TO PROGRAMMING

Vending Machines Lesson 5-1 Relations and Functions

Python for ArcGIS. Lab 1.

TUPLES AND RECURSIVE LISTS 5

Program Planning, Data Comparisons, Strings

Sequences and Loops. Indices: accessing characters in a string. Old friend: isvowel. Motivation: How to count the number of vowels in a word?

CSE 111 Bio: Program Design I Lecture 5: More Strings, Intro Lists, Translation & Central Dogma of Biology

Cpt S 122 Data Structures. Introduction to C++ Part II

MULTIPLE CHOICE. Chapter Seven

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Java Fall 2018 Margaret Reid-Miller

The Dynamic Typing Interlude

Lesson 11: Tessellate to understand perimeter as the boundary of a shape.

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

Examining the Code. [Reading assignment: Chapter 6, pp ]

Worksheet 6: Basic Methods Methods The Format Method Formatting Floats Formatting Different Types Formatting Keywords

Grouping Objects. Primitive Arrays and Iteration. Produced by: Dr. Siobhán Drohan. Department of Computing and Mathematics

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

Java Programming. Atul Prakash

PL/SQL Block structure

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

UNIT 5. String Functions and Random Numbers

Name: Partner: Python Activity 9: Looping Structures: FOR Loops

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

Lesson 13: The Graph of a Linear Equation in Two Variables

Good Luck! CSC207, Fall 2012: Quiz 1 Duration 25 minutes Aids allowed: none. Student Number:

Introduction to Computer Science. Course Goals. Python. Slides02 - Intro to Python.key - September 15, 2015

6 Subscripting. 6.1 Basics of Subscripting. 6.2 Numeric Subscripts. 6.3 Character Subscripts

dilations per 3.notebook October 28, 2015 Module 3 Similarity Dilations

Introduction to Programming Using Java (98-388)

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

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections

Language Reference Manual

CSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Control Flow: Loop Statements

Adding Integers. Unit 1 Lesson 6

The Big Python Guide

1.1. Real Number System Numbers, Numbers,... My Notes ACTIVITY

Working with Strings. Husni. "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc.

BEFORE WE GET STARTED. BASIC TERMS TO KNOW JavaScript Python Basic Syntax Object String Functions Arguments Property While- Loops Varibles

Introduction to String Manipulation

RETURN X return X Returning a value from within a function: computes the value of variable exits the function and returns the value of the variable

LOOPS. Repetition using the while statement

Introduction to Programming

Chief Reader Report on Student Responses:

LISTS WITH PYTHON. José M. Garrido Department of Computer Science. May College of Computing and Software Engineering Kennesaw State University

Introduction to Programming

[301] JSON. Tyler Caraza-Harter

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 A9 Recursion

Introduction to Computer Programming Using FORTRAN 77

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

How can you simplify? How can you get x alone? Is there more than one way to simplify? Is there always a solution? CPM Materials modified by Mr.

Operators in C. Staff Incharge: S.Sasirekha

ECE 2400 Computer Systems Programming Fall 2018 Topic 2: C Recursion

Topic 7: Lists, Dictionaries and Strings

Warm Up! Complete the warm-up questions on your warm-up paper. Solve each absolute value and represent them on a number line.

PROBLEM SOLVING 11. July 24, 2012

CSE 303: Concepts and Tools for Software Development

What is an algorithm?

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

Python Intro GIS Week 1. Jake K. Carr

Here n is a variable name. The value of that variable is 176.

CS 61A Discussion 8: Scheme. March 23, 2017

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

Chapter 9: Dealing with Errors

Transcription:

Fundamentals of Python: First Programs Chapter 4: Strings (Indexing, Slicing, and Methods)

Objectives After completing this lesson, you will be able to: 1) Know the definition of a string and that strings are "immutable objects", 2) Access individual characters in a string, 3) Retrieve a substring from a string, 4) Search for a substring in a string, and 5) Use string methods to manipulate strings. Fundamentals of Python: First Programs 2

Problem Statement Ask the user for their full name: first name, one space, middle name, one space, and last name, then press return. Your job is to separate this full name into three separate variables: one for the first name, one for the middle name, and a third for the last name. Display these separate names on the screen. Also display the last name, a comma, a space, and the first name. You will need to use Python s string manipulation features. Write a BRIEF algorithm in pseudo code indicating how you would separate the names. Fundamentals of Python: First Programs 3

Algorithm Discussion BRIEFLY discuss the essentials of the algorithm with your row partner. Start with a brief private thinking time We will use Listen & Compare One group share your algorithm. Fundamentals of Python: First Programs 4

Today s Lesson The How Reading a Technical Text Determine the central ideas of the text, summarizing the complex concepts, processes, and/or information presented in the text by paraphrasing them in simpler but still accurate terms. Determine the meaning of symbols, key terms, and Python commands. Fundamentals of Python: First Programs 5

Today s Lesson The What Strings: Definition, Indexing, Slicing, and String Methods Read Section 4.1 (Pages 122 125) AND Section 4.4 (Pages 136 140) Fundamentals of Python: First Programs 6

Today s Lesson The How Part 2 Start with private thinking time. We will use Listen & Compare Structured Discussion with your partner. Groups will share (Explain to your partner) What you learned including: The Definition of a String Characteristic (property vocabulary word) of a String How to use the string command/method and what type of problem could you use this command. Try it out in IDLE if you wish Fundamentals of Python: First Programs 7

Algorithm Discussion Part 2 List as many ways as possible to find the spaces and/or separate the names. If we wanted to error trap the Names program, which methods would we use and why would you use them? What order would you use the commands/methods? What about the in Operator? Fundamentals of Python: First Programs 8

Exit Ticket: Short Quiz Socrative.com Room number: LCHS607 If time permits, start program 4A and 4B Fundamentals of Python: First Programs 9

Accessing Characters and Substrings in Strings In this section, we examine the internal structure of a string more closely You will learn how to extract portions of a string called substrings Fundamentals of Python: First Programs 10

The Structure of Strings An integer can t be factored into more primitive parts A string is an immutable data structure Data structure: Consists of smaller pieces of data String s length: Number of characters it contains (0+) Fundamentals of Python: First Programs 11

The Subscript Operator The form of the subscript operator is: Examples: index is usually in range [0,length of string 1]; can be negative Fundamentals of Python: First Programs 12

The Subscript Operator (continued) Subscript operator is useful when you want to use the positions as well as the characters in a string Use a count-controlled loop Fundamentals of Python: First Programs 13

Slicing for Substrings Python s subscript operator can be used to obtain a substring through a process called slicing Place a colon (:) in the subscript; an integer value can appear on either side of the colon Fundamentals of Python: First Programs 14

Testing for a Substring with the in Operator When used with strings, the left operand of in is a target substring and the right operand is the string to be searched Returns True if target string is somewhere in search string, or False otherwise Fundamentals of Python: First Programs 15

String Methods Python includes a set of string operations called methods that make tasks like counting the words in a single sentence easy Fundamentals of Python: First Programs 16

String Methods (continued) A method behaves like a function, but has a slightly different syntax A method is always called with a given data value called an object Methods can expect arguments and return values A method knows about the internal state of the object with which it is called In Python, all data values are objects Fundamentals of Python: First Programs 17

String Methods (continued) Fundamentals of Python: First Programs 18

String Methods (continued) Fundamentals of Python: First Programs 19

String Methods (continued) Fundamentals of Python: First Programs 20

String Methods (continued) Fundamentals of Python: First Programs 21

String Methods (continued) Example: extracting a filename s extension The subscript [-1] extracts the last element Can be used to write a general expression for obtaining any filename s extension, as follows: Fundamentals of Python: First Programs 22

Summary A string is a sequence of zero or more characters Immutable data structure [] used to access a character at a given position Can also be used for slicing ([<start>:<end>]) in operator is used to detect the presence or absence of a substring in a string Method: operation that is used with an object The string type includes many useful methods for use with string objects Fundamentals of Python: First Programs 23