Computing with Strings CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.

Slides:



Advertisements
Similar presentations
Programming Patterns CSC 161: The Art of Programming Prof. Henry Kautz 9/30/2009.
Advertisements

CS 100: Roadmap to Computing Fall 2014 Lecture 0.
An Introduction to Python – Part II Dr. Nancy Warter-Perez.
Lists Introduction to Computing Science and Programming I.
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”?
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”?
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.
Recitation 1 Programming for Engineers in Python.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
Working with Files CSC 161: The Art of Programming Prof. Henry Kautz 11/9/2009.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Bell Ringer What types are numbers are there is the python programming language?
CS 100: Roadmap to Computing Fall 2014 Lecture 01.
Computing with Numbers CSC 161: The Art of Programming Prof. Henry Kautz 9/14/2009.
Lists in Python.
An Overview of Programming in Python CSC 161: The Art of Programming Prof. Henry Kautz 9/9/2009 Slides stolen shamelessly from Dr. Mark Goadrich, Centenary.
Data Collections: Dictionaries CSC 161: The Art of Programming Prof. Henry Kautz 11/4/2009.
Files and Dictionaries CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
Python for Informatics: Exploring Information
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
60 Questions (review for final exam) CSC 161: The Art of Programming Prof. Henry Kautz 12/7/
Computer Science 101 Introduction to Programming.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
Strings CS303E: Elements of Computers and Programming.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
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.
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.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Python Conditionals chapter 5
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Strings in Python. Computers store text as strings GATTACA >>> s = "GATTACA" s Each of these are characters.
Input and Output CMSC 120: Visualizing Information Lecture 4/10.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Variables, Types, Expressions Intro2CS – week 1b 1.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Fundamental Programming Fundamental Programming More Expressions and Data Types.
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.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from.
Chapter 4 Computing With Strings Charles Severance Textbook: Python Programming: An Introduction to Computer Science, John Zelle.
Chapter 2 Writing Simple Programs
CSc 120 Introduction to Computer Programing II Adapted from slides by
CS 100: Roadmap to Computing
CSc 120 Introduction to Computer Programing II
CISC101 Reminders Quiz 2 this week.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Python - Strings.
Strings Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Data types Numeric types Sequence types float int bool list str
CS 100: Roadmap to Computing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Strings in Python.
CS1110 Today: collections.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CISC101 Reminders Assignment 2 due today.
Introduction to Python Strings in Python Strings.
Introduction to Computer Science
CS 100: Roadmap to Computing
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
CS 100: Roadmap to Computing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Presentation transcript:

Computing with Strings CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009

Some of My Favorite Things Things made of strings Human language Web pages Databases Programs... Things to do with strings Translate Compose Format Analyze... 2

String Data Type The data type: Sequence of characters of any length In expressions, enclose in single or double quotes: "Henry A. Kautz" 'Henry A. Kautz' Can be assigned to a variable: >>> name = Henry >>> print name Henry Not the same a number, even if they print the same! >>> x = "99" >>> y = 99 >>> print x == y False 3

Reading Strings Reading a string is little bit trickier than reading numbers Python input() function Reads a line of text Evaluates it as a Python expression Returns the value This is why typing a number (or any numeric expression) gives a number, not a string >>> x = input("Enter number:") Enter number: 99 >>> print x == 99 True >>> print x == "99" False 4

Reading Strings In fact, input() accepts any legal expression: >>> x = input("Enter number:") Enter number: 99 / 3 >>> print x 33 >>> y = 25 >>> x = input("Enter number:") Enter number: y >>> print x 25 5

Reading Strings Why does this fail? >>> name = input("Enter first name: ") Enter first name: Henry Traceback (most recent call last): File " ", line 1, in name=input("Enter first name:") File " ", line 1, in NameError: name 'Henry' is not defined >>> 6

Reading Strings Clumsy solution: >>> name = input("Enter first name: ") Enter first name: "Henry" >>> Better solution: Use raw_input() Like input(), but does not evaluate: always returns a string >>> name = raw_input("Enter first name: ") Enter first name: Henry >>> type(name) 7

Operations on Strings Indexing Just like lists, can use square brackets to pick out characters within a string >>> course = 'Art of Programming' >>> course[0] 'A' Slices of strings can be addressed >>> course[0:3] 'Art' >>> course[7:] 'Programming' >>> course[7:len(course)] 'Programming' 8

Operations on Strings Concatenation >>> city = 'Rochester' >>> state = 'NY' >>> address = city + state >>> address RochesterNY >>> address = city + ' ' + state >>> address Rochester NY >>> zip = >>> address = city + ' ' + state + ' ' + zip TypeError: cannot concatenate 'str' and 'int' objects >>> address = city + ' ' + state + ' ' + str(zip) >>> address Rochester NY str() converts almost anyththing to a string

Strings and Lists Compare string and list: mystring = "Cat" mylist = ['C', 'a', 't'] Can index elements or slices mystring[0:2] == 'Ca' mylist[0:2] == ['C', 'a'] Can concatenate mystring + 's' == 'Cats' mylist + ['s'] == ['C', 'a', 't', 's'] 10

Differences Between Strings & Lists You can change an element of a list: mylist[0] = 'B' mylist == ['B', 'a', 't'] You cannot change a character in a string: mystring[0] = 'B' ERROR Instead, slice and concatenate to create a new string: 'B' + mystring[1:3] == 'Bat' 11

String Library import string string.split('To be, or not to be, that is the question.') ['To', 'be,', 'or', 'not', 'to', 'be,', 'that', 'is', 'the', 'question'] string.split('To be, or not to be, that is the question.', ',') ['To be', ' or not to be', ' that is the question.'] string.join(['To', 'be,', 'or', 'not', 'to', 'be,', 'that', 'is', 'the', 'question']) 'To be, or not to be, that is the question.' 12

Converting Between Strings & Lists list('cat') ['c', 'a', 't'] str(['c', 'a', 't']) "['c', 'a', 't']" string.join(['c', 'a', 't']) 'c a t' string.join(['c', 'a', 't'], "") 'cat' 13

More Library Functions Finding the position of one string within another string string.find('To be or not?', 'be') 3 Replacing a string with another string string.replace('Sigh. I wish. To sleep.', '.', '!') 'Sigh! I wish! To sleep!' See the textbook many more string functions 14

In Class Exercise With a person sitting next to you, write Python to reverse the string stored in the variable S. Left side of class: do NOT use lists, only strings. Right side of class: convert to list, reverse, then convert back to a string. 15

Course Status Quiz Wednesday in class for, if, logical expressions, range, lists 15 minutes Assignment 3: RNA Secondary Structure Prediction Design solution in your workshop this week Implement & debug in lab Tuesday or Thursday Turn in by 10:00am Saturday Read: Zelle, Chapter 4 16