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.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Container Types in Python
CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
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.
Tuples. Tuples 1 A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The only difference is that tuples can't be.
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.
STRINGS IN PYTHON. Where have we seen strings before? #string type variables s = “hello” #obtaining keyboard input from the user choice = input(“Please.
I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
Lists Introduction to Computing Science and Programming I.
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
Computer Science 111 Fundamentals of Programming I Sequences: Strings.
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.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 8 More on Strings and Special Methods 1.
Python Programming Chapter 7: Strings Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Built-in Data Structures in Python An Introduction.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
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:
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
More Strings CS303E: Elements of Computers and Programming.
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
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
Chapter 10 Loops: while and for CSC1310 Fall 2009.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
7. Lists 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Python Strings. String  A String is a sequence of characters  Access characters one at a time with a bracket operator and an offset index >>> fruit.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
CPSC 217 T03 Week IX Part #1: Function, Iteration and List Hubert (Sathaporn) Hu.
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.
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.
Computer Science 18 Feb 2011 Strings in Python. Introduction Prior experience  Defining string variables  Getting user input  Printing strings Lesson.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
String and Lists Dr. José M. Reyes Álamo.
Sequences and Indexing
Module 4 String and list – String traversal and comparison with examples, List operation with example, Tuples and Dictionaries-Operations and examples.
Tuples and Lists.
Chapter 4 Strings & Tuples
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
CS 115 Lecture 8 Structured Programming; for loops
Section 6: Sequences Chapter 4 and 5.
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Bryan Burlingame 03 October 2018
Bryan Burlingame 17 October 2018
Guide to Programming with Python
Data types Numeric types Sequence types float int bool list str
8 – Lists and tuples John R. Woodward.
CMSC201 Computer Science I for Majors Lecture 12 – Tuples
CSC 221: Introduction to Programming Fall 2018
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
CMSC201 Computer Science I for Majors Lecture 08 – For Loops
Topics Sequences Introduction to Lists List Slicing
Basic String Operations
For loops Taken from notes by Dr. Neil Moore
Topics Sequences Lists Copying Lists Processing Lists
Topics Basic String Operations String Slicing
Bryan Burlingame 13 March 2019
Introduction to Computer Science
Topics Sequences Introduction to Lists List Slicing
Python Review
Topics Basic String Operations String Slicing
Topics Basic String Operations String Slicing
Presentation transcript:

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. A sequence length is the number of elements it has. Ex. string = “Hello World!” len(string)  12 What would be the length of the string “Hi my name is Jim.”?

In Operator You can use the in operator anywhere in your programs to check if an element is a member of a sequence. _______ in _______ element sequence Ex. If “e” in message: print (“is in your message”) else: print(“is not in your message”) If it is a member it is True, else it is False

Indexing Strings When you use a for loop you are using sequential access. This is like wanting to get to the box at the bottom of a pile and having to lift all of the heavy boxes on the top first before you make it to the bottom. When you index strings you are using random access. This is like just being able to grab the box you want without having to lift any other boxes. *Random Access Program

Indexing Strings Continued Note: Positions start at 0, so the last element in a sequence is at the position # of its length minus 1. Ex. In the string “heart” the length is 5 so the letter t would be in position 4. Working with Negative Position Numbers -With negative position numbers you count backwards starting with the last letter. Ex. String = “pizza pie” string [-5] = 1 string [-4] = “ “ string [-1] = e

String Immutability Once you create a string, you can’t change it. (Imagine writing on paper in ink) If you were to say word = “game” word [0] = “l” You will get an error. You can’t change the g in game to an l. Note: This is not the same as name = “Ms”. A then name = “Jim”. Here you are just assigning the same variable a different name.

Building a New String Even though you can’t change a string, you can build a new string one element at a time. No Vowels Program: # No Vowels # Demonstrates creating new strings with a for loop message = input("Enter a message: ") new_message = "" VOWELS = "aeiou” print() for letter in message: if letter.lower() not in VOWELS: new_message += letter print("A new string has been created:", new_message) print("\nYour message without vowels is:", new_message) input("\n\nPress the enter key to exit.") VOWELS is a constant that is intentionally capitalized. This is an indication that this string should not change Throughout the program. letter.lower() will change each letter in message to lowercase since you don’t care about the case when you are checking to see whether the letter is a vowel. Concatenates a new string adding each letter that is not a vowel as the loop runs.

Slicing Strings With indexing, you don’t have to copy just one element at a time, you can make copies of continuous sections of elements (called slices). With slicing you can grab anything from a one element to a group of elements to the entire string. Ex. Pizza Slicer Programp i z z a word = “pizza” word [0:5]  pizza word [1:3]  iz word [-4:-2]  iz word [-4:3]  iz

Be Careful! Note: If you create an “impossible” slice, where the starting point is bigger than the ending point, like word [2:1], you won’t cause an error. Instead, Python will quietly return an empty sequence. For strings, that means you’ll get the empty string. So be careful, because this is probably not the kind of result you’re after.

Slicing Shorthand You can omit the beginning point for the slice to start the slice at the beginning of the sequence. p i z z a Ex. word[:4] is the same as word[0:4] You can omit the ending point for the slice so that it ends with the last element. Ex. word[2:] is just shorthand for word [2:5] You can even omit both numbers to get a slice that is the entire sequence. Ex. word[:] is shorthand for word[0:5]