Bryan Burlingame 17 October 2018

Slides:



Advertisements
Similar presentations
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Advertisements

Lists Introduction to Computing Science and Programming I.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Guide to Programming with Python
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Lists in Python.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Introducing Python CS 4320, SPRING Format: Field widths and Alignment The string representation of a value can be padded out to a specific width.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
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.
Built-in Data Structures in Python An Introduction.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Lists CS303E: Elements of Computers and Programming.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
Introduction to Strings CSIS 1595: Fundamentals of Programming and Problem Solving 1.
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
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
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.
CSI 32 Lecture 20 Chapter 8 Input, Output, and Files 8.1 Standard Input and Output (review) 8.2 Formatted Strings 8.3 Working with Files 8.5 Case Studies.
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
String and Lists Dr. José M. Reyes Álamo.
CMSC201 Computer Science I for Majors Lecture 14 – Tuples
Containers and Lists CIS 40 – Introduction to Programming in Python
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Bryan Burlingame 26 September 2018
Formatted Input/Output
Chapter 7: Strings and Characters
Learning to Program in Python
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Bryan Burlingame 03 October 2018
Bryan Burlingame Halloween 2018
Lecture 5 Fruitful Functions
Creation, Traversal, Insertion and Removal
Lecture 2 Python Programming & Data Types
Week 9 – Lesson 1 Arrays – Character Strings
Bryan Burlingame 28 November 2018
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Lists in Python Outputting lists.
Topics Sequences Introduction to Lists List Slicing
Lecture 2 Python Programming & Data Types
CMSC201 Computer Science I for Majors Lecture 18 – String Formatting
Fundamentals of Python: First Programs
CS190/295 Programming in Python for Life Sciences: Lecture 3
Formatted Input/Output
CS1110 Today: collections.
15-110: Principles of Computing
Bryan Burlingame 6 March 2019
Topics Basic String Operations String Slicing
Bryan Burlingame 13 March 2019
Formatted Input/Output
Introduction to Computer Science
Bryan Burlingame Halloween 2018
Bryan Burlingame 17 April 2019
Topics Sequences Introduction to Lists List Slicing
Introduction to Computer Science
Bryan Burlingame 24 April 2019
Topics Basic String Operations String Slicing
Lecture 13 Teamwork Bryan Burlingame 1 May 2019.
Topics Basic String Operations String Slicing
Introduction to Computer Science
Presentation transcript:

Bryan Burlingame 17 October 2018 Lecture 9 Strings Bryan Burlingame 17 October 2018

Announcements No homework due up front Homework 7 due next week Extra week for Arduino Lab (Lab 7) Light lab this week to give time to fix Lab 7 Read Chapters 8, 9 Let’s talk about due date Midterm problem to be posted today Announcements

Learning Objectives Introduce and discuss strings Identify the similarities and differences between strings and lists

Lists and Sequences Recall: lists are a sequence of values connected by a common name Lists have many methods to manipulate their contents Methods are functions attached to some object and are accessed via the . operator

Remember: the index starts at zero Strings Like a list, a string is a sequence of values, specifically a sequence of characters Basic string initialization uses quotes, either single ‘’ or double “” As with lists, individual letters can be accessed via the [] operator Remember: the index starts at zero

len Many of the tools and techniques used with lists, also work with strings len() also works on strings much like it does on lists, returning the number of characters of the string

Traversal Recall: traversing a collection is the process of working on the collection element by element As with lists, either a for loop or a while loop can be used to traverse a string

Slicing a string The slice operator [:] works on strings just as it does on lists For string[a:b] return the characters from a to (b-1) If a is omitted, assume 0 If b is omitted, assume the end of the string If both are omitted, return a copy of the string

Mutability Recall: lists are mutable – the elements of the list can be changed Strings are immutable. The characters of a string cannot be changed The “grades[1] = 72” line passes, the error is on the next line

Mutability Recall: lists are mutable – the elements of the list can be changed Strings are immutable. The characters of a string cannot be changed The “grades[1] = 72” line passes, the error is on the next line Use slices and concatenation, instead

Search Search is the process of traversing a sequence, evaluating each member of that sequence for some value, and returning when the value is found From the text:

Strings Methods Strings have many methods Since strings are immutable, many methods return a string Note: slices of a string are considered strings as well https://docs.python.org/3/library/stdtypes.html#string-methods

String Formatting Strings are generally intended to be consumed by people. Attractive and consistent formatting makes this easier Python has a mini-language just for formatting Takes the form “Format Spec”.format(thing_to_be_formatted) Each element of the format list is matched in order with a replacement field {#} {0} == a, {1} == b, {2} == c, where the format is specified within the {} and between the {} (note the spaces in the format string and the output) https://docs.python.org/3/library/string.html#formatstrings

String Formatting - Examples Note how the values follow the replacement field and the inter-replacement field characters are added

String Formatting - Examples Formatting of the values are controlled in the replacement field {#:format_spec} Ex: {0:.2f} – format the 0th value, display as a float with 2 decimal places

String Formatting - General Each of the options on the format_spec line can be included Print a minimally 10 character wide field, field. Center the value. Include the + sign, show as a 2 decimal point float. Fill in any white space with an asterisk * https://docs.python.org/3/library/string.html#formatstrings

String Formatting - General Each of the options on the format_spec line can be included Print a minimally 10 character wide field, field. Center the value. Include the + sign, show as a 2 decimal point float. Fill in any white space with an asterisk * https://docs.python.org/3/library/string.html#formatstrings

String Formatting - General All the values in the format_spec are optional Go to the docs to understand what each value controls Note that this is a very succinct mini-language https://docs.python.org/3/library/string.html#formatstrings

String Formatting - General The format_spec is simply a string; therefore, it can be transformable

Resources Downey, A. (2016) Think Python, Second Edition Sebastopol, CA: O’Reilly Media (n.d.). 3.7.0 Documentation. String Methods — Python 3.7.0 documentation. Retrieved October 16, 2018, from https://docs.python.org/3/library/stdtypes.html#string-methods