Computer Science 18 Feb 2011 Strings in Python. Introduction Prior experience  Defining string variables  Getting user input  Printing strings Lesson.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
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.
An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.
Variables and I/O. Types Strings –Enclosed in quotation marks –“Hello, World!” Integers –4, 3, 5, 65 Floats –4.5, 0.7 What about “56”?
Introduction to Python
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.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Python Programming Chapter 7: Strings Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
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:
More Strings CS303E: Elements of Computers and Programming.
© Oxford University Press All rights reserved. CHAPTER 6 STRINGS.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Introduction to Strings CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Mr. Fowler Computer Science 14 Feb 2011 Strings in Python.
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.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
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
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
INLS 560 – S TRINGS Instructor: Jason Carter. T YPES int list string.
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.
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.
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.
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.
Input and Output Upsorn Praphamontripong CS 1110
Tuples and Lists.
Section 3.2c Strings and Method Signatures
Visual Basic .NET BASICS
Creation, Traversal, Insertion and Removal
Chapter 8 More on Strings and Special Methods
Python - Strings.
4. sequence data type Rocky K. C. Chang 16 September 2018
Chapter 8 More on Strings and Special Methods
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
Python Basics with Jupyter Notebook
Topics Basic String Operations String Slicing
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
COMPUTING.
Presentation transcript:

Computer Science 18 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 so far Strings are generally used to store text data  sample="LEVEL"  Name=input ("What is your name?") Can also store non-natural language data e.g. telephone numbers  my_tel="+1 (301) " Q: What data should/should not be stored in strings?

Strings vs. Integers Both strings and integer variables can store numbers  my_speed_str="300"  my_speed_integer=300 Q: What is the difference between strings and integers?

Strings in use Q: Where are strings displayed and entered?

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 (including empty).  String are enclosed in quotation marks.  str_var = "300 m/s"  empty_str=""

Length of a string Use len(str) to return string length (# of characters)  sample="SERIES"  len(sample)=  empty_str=""  len(empty_str) = 0 6

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[ ]  just_v=ss[ ]  Python strings are immutable (can’t change individual chars)  s[0]="B" Try it out 2 0 

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“  zeroZero = speed_display[ : ] 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 Slices (sections of a 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 it out: Build a string  build=""  while len(build)<5:  build = build +"a"  print build =40 =“2020”

String comparison To test for equality, use the == operator To compare order, use the operators user_name=raw_input("Enter your name- ") if user_name==“Sam": print "Welcome back Sam“ elif user_name<"Sam": print "Your name is before Sam“ else: print "Your name is after Sam“ These operators are case sensitive. Upper case characters are ‘less than’ lower case

Operations: Traversing through a string Use a loop to examine each character in a string See HTTLCS for an alternative format: for inHTTLCS strng="count # of us" index=0 count=0 while index < len(strng): if strng[index] == "u“ or strng[index] == “U": count+=1 index+=1 Try it out  How would we traverse backwards?

Summary Strings are composed of characters len(sample) String length sample[i] Character at index i 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

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