Mr. Fowler Computer Science 14 Feb 2011 Strings in Python.

Slides:



Advertisements
Similar presentations
Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
Advertisements

Java Programming Strings Chapter 7.
Intro to Python Welcome to the Wonderful world of GIS programing!
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.
Computer Science 111 Fundamentals of Programming I Sequences: Strings.
Guide to Programming with Python
Group practice in problem design and problem solving
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.
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.
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.
07/10/ Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Strings CS303E: Elements of Computers and Programming.
Fundamentals of Python: First Programs
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition.
6 Strings and Tuples © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming 1.
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:
You Need an Interpreter!. Closing the GAP Thus far, we’ve been struggling to speak to computers in “their” language, maybe its time we spoke to them in.
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 Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Introduction to Strings CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Variables, Input, and Output. Challenge: ● Ask the user his or her name, and repeat that name to the user ● Pause video and try.
Strings The Basics. Strings a collection data type can refer to a string variable as one variable or as many different components (characters) string.
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
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.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
02/03/ Strings Left, Right and Trim. 202/03/2016 Learning Objectives Explain what the Left, Right and Trim functions do.
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.
String & Exception. Lesson plan Class & Object String Exercise for midterm.
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.
String Manipulation Part 1
Tuples and Lists.
Lesson 07: Strings Topic: Introduction to Programming, Zybook Ch 6, P4E Ch 6. Slides on website.
Creation, Traversal, Insertion and Removal
Chapter 8 More on Strings and Special Methods
Chapter 8 More on Strings and Special Methods
4. sequence data type Rocky K. C. Chang 16 September 2018
Chapter 8 More on Strings and Special Methods
Introduction to Programming
4.1 Strings ASCII & Processing Strings with the Functions
Web DB Programming: PHP
CSC 221: Introduction to Programming Fall 2018
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Representation and Manipulation
Fundamentals of Python: First Programs
Basic String Operations
CHAPTER 3: String And Numeric Data In Python
Topics Basic String Operations String Slicing
7 – Variables, Input and Output
Bryan Burlingame 13 March 2019
String Processing 1 MIS 3406 Department of MIS Fox School of Business
Introduction to Computer Science
Introduction to Computer Science
Topics Basic String Operations String Slicing
By Himanshi dixit 11th ’B’
Topics Basic String Operations String Slicing
Introduction to Computer Science
Presentation transcript:

Mr. Fowler Computer Science 14 Feb 2011 Strings in Python

Introduction Prior experience  Defining string variables  Getting user input  Printing strings Lesson Objectives  Understand string structure  Access characters and sub-sections from a string  Concatenate strings  Traverse a string

Strings in use Strings are generally used to store text data  sample=“LEVEL”  name=raw_input (“What is your name?”) Can also store non-natural language data e.g. telephone numbers  my_tel=“+1 (301) ”  my_speed=“300 m/s” Q: What is the difference between strings and integers?

Strings in use Strings are generally used to store text data  sample=“LEVEL”  name=raw_input (“What is your name?”) Can also store non-natural language data e.g. telephone numbers  my_tel=“+1 (301) ” Q: What data should (not) be stored in strings?

String composition Strings are a composite data type  Built from another data type, characters  In a string, characters are stored in sequence.  Strings can be any length (or empty).  String constants are enclosed in double quotes.  str_var = “300 m/s”  empty_str=“”

String length Use len() to return the length of a string  sample=“SERIES”  len(sample)=  empty_str=“”  len(empty_str) =

String representation In strings, characters are accessed by index  …like mailboxes in an apartment building.  First index is 0, not 1.  s=“LEVEL”  startChar=s[0]  just_v=s[ ]  Python strings are immutable (can’t change characters).  s[0]=“B” Try it out

String subsections Use a range to specify a slice (sub-string)  from start index up to but not including the last index  speed_display = “300 m/s”  middle_two_characters= speed_display[1:3] Omit the first value to select the start of a string  just_num= speed_display[:_] Omit the second value to select the end of a string  just_unit = speed_display[_:] Try it out

String operations: Concatenation Combine strings using + (concatenation operator) full_name = “Henry” + “ “ + “James” print “:” + full_name + ”:” Concatenation is not addition  vision_str=“20”+”20”  vision_val=20+20 Try building a string  build=“”  while len(build)<5:  build = build +”a”  print build

String comparison To test for equality, use the == operator user_name=raw_input(“Enter your username?”) if user_name==“Brad”: print “Welcome back Bradley” To compare order, use the operators if user_name<“Sam”: print “Your name is before Sam’s in the phone book” These operators are case sensitive. Upper case characters are ‘less than’ lower case Try it out

Traversing through a string Use a loop to examine each character in a string strng=“count the number of u’s in this string” index = 0 count=0 while index < len(strng): if strng[index] == “u” count+=1 How would we traverse backwards? Try it out See HTTLCS for an alternative format: for in

Summary Strings are composed of characters len(sample) :String length sample[i] :Character at index i (starts at 0) sample[start:end] :Slice from start up to but not including end index sample+sample : Concatenate strings sample==“test” : Test for equality sample<test: Test for order More details and exercises: HTLLCS Ch 7HTLLCS Ch 7

Advanced Strings

String operations: find find() searches for a string within a string To use it, insert this at the top of your code:  import string find() returns the first index of a substring  full_name = “Henry James”  string.find(full_name,”e”) You can specify the starting point of the search: string.find(full_name,”e”,2) If the string is not found, find() returns -1 find() is case sensitive