Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.

Slides:



Advertisements
Similar presentations
Python Basics: Statements Expressions Loops Strings Functions.
Advertisements

CS 100: Roadmap to Computing Fall 2014 Lecture 0.
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.
Chapter 4 Working with Strings. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Sequence of characters.
Lists Introduction to Computing Science and Programming I.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Introduction to Programming Workshop 2 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
 Pearson Education, Inc. All rights reserved Arrays.
Strings CS303E: Elements of Computers and Programming.
Chapter 7 Lists and Tuples. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Data Structures.
Fundamentals of Python: First Programs
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.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Ch. 10 For Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
COSC 235: Programming and Problem Solving Ch. 2: Your first programs!!! Instructor: Dr. X.
Chapter 4 Working with Strings. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Sequence of characters.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
By Austin Laudenslager AN INTRODUCTION TO 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.
Advanced Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
INLS 560 – S TRINGS Instructor: Jason Carter. T YPES int list string.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Strings … operators Up to now, strings were limited to input and output and rarely used as a variable. A string is a sequence of characters or a sequence.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
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.
Introduction to Programming
String and Lists Dr. José M. Reyes Álamo.
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Strings Part 1 Taken from notes by Dr. Neil Moore
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Strings
Introduction to Strings
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Programming
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Basic String Operations String Slicing
Introduction to Computer Science
Introduction to Strings
Python Review
Topics Basic String Operations String Slicing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Class code for pythonroom.com cchsp2cs
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Topics Basic String Operations String Slicing
Introduction to Computer Science
Presentation transcript:

Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1

Last week we talked about problem solving What’s the difference between an algorithm and a program? Compute the number of times a given digit D appears in a given number N.  The number of times 5 appears in 1550 is 2.  The number of times 0 appears in 1550 is 1.  The number of times 3 appears in 1550 is 0.

The algorithm Set a counter to zero Look at each digit in the number, one at a time  Increment the counter every time we see a digit that equals our target value Print the final results

The Program See the code This particular implementation uses mathematics to break the number down into single digits. But we also could do that if we know how to work with Strings.

Introduction to Strings A string is a sequence of characters. A string is indicated between ‘ ‘ or “ “ The exact sequence of characters is maintained.

The Index Because the elements of a string are a sequence, we can associate each element with an index, a location in the sequence:  Non-negative values count up from the left, beginning with index 0  Negative values count down from the right, starting with -1

Accessing an Element A particular element of the string is accessed by the index of the element surrounded by square brackets [ ] helloStr = ‘Hello World’ print (helloStr[1]) => prints ‘e’ print (helloStr[-1]) => prints ‘d’ print (helloStr[11]) => ERROR

Slicing: the Rules Slicing is the ability to select a subsequence of the overall sequence Uses the syntax [start : finish], where:  start is the index of where we start the subsequence  finish is the index of one after where we end the subsequence If either start or finish are not provided, it defaults to the beginning of the sequence for start and the end of the sequence for finish

Half Open Range for Slices Slicing uses what is called a half-open range The first index is included in the sequence The last index is one after what is included

© 2011 Pearson Addison-Wesley. All rights reserved.

Basic String Operations s = ‘spam’ length operator len() len(s)  4 + is concatenate newStr = ‘spam’ + ‘-’ + ‘spam-’ print (newStr)  spam-spam- * is repeat, the number is how many times newStr * 3  spam-spam-spam-spam-spam-spam-

Some Details Both + and * on strings make a new string, but does not modify the arguments. Order of operation is important for concatenation and repetition. The types required are specific. For concatenation you need two strings; for repetition, a string and an integer.

What Does A + B Mean? What operation does the above represent? It depends on the types!  two strings, concatenation  two integers addition The operator + is overloaded.  the operation + performs depends on the types it is working on

The type function You can check the type of the value associated with a variable using type myStr = ‘hello world’ type(myStr)  yields myStr = 245 type(myStr)  yields

Strings are Immutable Strings are immutable, that is you cannot change one once you make it:  aStr = ‘spam’  aStr[1] = ‘l’  ERROR However, you can use it to make another string (copy it, slice it, etc).  newStr = aStr[:1] + ‘l’ + aStr[2:]  aStr  ‘spam’  newStr => ‘slam’

Iteration Through a Sequence To date, we have seen the while loop as a way to iterate over a suite (a group of python statements) We briefly touched on the for statement for iteration, such as the elements of a list or a string

for Statement We use the for statement to process each element of a list, one element at a time: for item in sequence: suite

What for means myStr=‘abc’ for myVar in myStr: print (myVar) first time through, myVar=‘a’ (myStr[0]) second time through, myVar=‘b’ (myStr[1]) third time through, myVar=‘c’ (myStr[2]) no more sequence left, we quit

Power of the for Statement Sequence iteration as provided by the for statement is very powerful and very useful in Python. Allows you to write some very “short” programs that do powerful things.

So how does that help us with our digit problem? Compute the number of times a given digit D appears in a given number N.  The number of times 5 appears in 1550 is 2.  The number of times 0 appears in 1550 is 1.  The number of times 3 appears in 1550 is 0.

String Function: len The len function takes as an argument a string and returns an integer, the length of a string. myStr = ‘Hello World’ len(myStr)  11 # space counts

Another version of the for loop myStr=‘abc’ for index in range(len(myStr)): print (myStr[index]) first time through, index=0 (myStr[0]) second time through, index=1 (myStr[1]) third time through, index=2(myStr[2]) no more numbers left, so we quit