Bryan Burlingame 13 March 2019

Slides:



Advertisements
Similar presentations
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
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.
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.
Lists Introduction to Computing Science and Programming I.
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.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Introducing Python CS 4320, SPRING Format: Field widths and Alignment The string representation of a value can be padded out to a specific width.
Built-in Data Structures in Python An Introduction.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
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.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
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.
Mr. Fowler Computer Science 14 Feb 2011 Strings in Python.
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
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python, Class 2 Karsten Hokamp, PhD Genetics TCD, 17/11/2015.
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
LECTURE 5 Strings. STRINGS We’ve already introduced the string data type a few lectures ago. Strings are subtypes of the sequence data type. Strings are.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
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
© 2016 Pearson Education, Ltd. All rights reserved.
Containers and Lists CIS 40 – Introduction to Programming in Python
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Announcements 2nd homework is due this week Wednesday (October 18)
Bryan Burlingame 26 September 2018
Chapter 7: Strings and Characters
Learning to Program in Python
Bryan Burlingame 03 October 2018
Bryan Burlingame Halloween 2018
Lecture 5 Fruitful Functions
CHAPTER THREE Sequences.
Lecture 2 Python Programming & Data Types
Bryan Burlingame 17 October 2018
Teaching London Computing
Announcements 3rd homework is due this week Wednesday (March 15)
Bryan Burlingame 28 November 2018
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Lists in Python Outputting lists.
Lecture 2 Python Programming & Data Types
CMSC201 Computer Science I for Majors Lecture 18 – String Formatting
Fundamentals of Python: First Programs
15-110: Principles of Computing
Bryan Burlingame 6 March 2019
CISC101 Reminders Assignment 2 due today.
Topics Basic String Operations String Slicing
Introduction to Computer Science
Bryan Burlingame Halloween 2018
Bryan Burlingame 17 April 2019
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 13 March 2019 Lecture 7 Strings Bryan Burlingame 13 March 2019

Announcements Homework 5 due up front Homework 6 due next week Begin Arduino next week Read Chapters 9, 10, Automate the Boring Stuff Chapter 7 Announcements

Learning Objectives Introduce and discuss strings & sequences

Remember: the index starts at zero Strings A string is a sequence of values, specifically a sequence of characters Basic string initialization uses quotes, either single ‘’ or double “” Individual letters can be accessed via the [] operator and some index Remember: the index starts at zero

len len() returns the number of characters of the string

Traversal Traversing a collection is the process of working on the collection element by element Either a for loop or a while loop can be used to traverse a string The for loops is more natural

Slicing a string The slice operator [:] can extract substrings from a string 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 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 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

Mutability 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 There are mutable collections, we’ll discuss some of them soon.

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