Strings and the slice operator

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

Container Types in Python
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.
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Chapter 6: User-Defined Functions I
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
Python Control of Flow.
Methods. Why methods? A method gives a name to something that you want to do, so you don’t have to think about how to do it, you just do it The name should.
Lists in Python.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Chapter 2 - Algorithms and Design
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat.
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.
Lesson 5: Working with Formulas and Functions Logical and 3D Formulas.
Built-in Data Structures in Python An Introduction.
COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve.
For loops in programming Assumes you have seen assignment statements and print statements.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
9/21/2015BCHB Edwards Python Data Structures: Lists BCHB Lecture 6.
More Strings CS303E: Elements of Computers and Programming.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Python – May 12 Recap lab Chapter 2 –operators –Strings –Lists –Control structures.
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.
Printing in Python. Printing Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
9.3 Equations and Absolute Value Goal(s): To solve equations involving absolute value.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
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.
String and Lists Dr. José M. Reyes Álamo.
Chapter 6: User-Defined Functions I
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Pamela Moore & Zenia Bahorski
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Functions.
Strings Part 1 Taken from notes by Dr. Neil Moore
Formatted and Unformatted Input/Output Functions
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company. 1
Introduction to Strings
Object Oriented Programming (OOP) LAB # 8
Python 17 Mr. Husch.
File Handling Programming Guides.
Lists in Python.
Data types Numeric types Sequence types float int bool list str
Strings and Lists – the split method
String and Lists Dr. José M. Reyes Álamo.
Python Data Structures: Lists
Python 17 Mr. Husch.
Domain and Range.
Recap Week 2 and 3.
Chapter 6: User-Defined Functions I
15-110: Principles of Computing
For loops Taken from notes by Dr. Neil Moore
Topics Basic String Operations String Slicing
Python Data Structures: Lists
Python Data Structures: Lists
Introduction to Computer Science
Python Review
Topics Basic String Operations String Slicing
Class code for pythonroom.com cchsp2cs
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Topics Basic String Operations String Slicing
Presentation transcript:

Strings and the slice operator

The slice operator This is the “substring” method which most languages have Given a string, you can slice pieces of the string the syntax of the operator uses the square brackets s[5:7] means the part of the string s which starts at position 5 and includes everything up to but not including position 7, in other words, the characters at position 5 and at position 6 Omitting an argument before the colon means to start at the beginning of the string s[:3] means s[0], s[1] and s[2] Omitting an argument after the colon means to include the rest of the string s[4:] means s[4], s[5]… up to and including the end of the string

slice returns a new string Suppose s were “abcdef” and you wrote an expression like s[1:4] Its value is “bcd”, a new string (the original string is not changed at all) If the expression were on a line by itself, it does nothing! You need to use the expression IN some statement, like an assignment statement, an if statement, a while statement, a print statement print(s[1:4]) makes sense t = s[1:4] makes a copy of the 3 characters as a string and puts it into t s = s[1:4] this does the same as the statement above, but the previous value of s is also discarded if s[1:4] == “bcd”: would result in True

Be careful of your range str[i:i+1] is the same as saying str[i] It is possible to go out of range with slice – don’t go past the end of the string! You can leave off both the starting and ending points, as in s[:]. This produces a copy of the whole string.