Strings CS303E: Elements of Computers and Programming.

Slides:



Advertisements
Similar presentations
Container Types in Python
Advertisements

Python Basics: Statements Expressions Loops Strings Functions.
CS 100: Roadmap to Computing Fall 2014 Lecture 0.
An Introduction to Python – Part II Dr. Nancy Warter-Perez.
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”?
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Python November 14, Unit 7. Python Hello world, in class.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
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
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Computing with Strings CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
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.
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CS 1 with Robots Variables, Data Types & Math Institute for Personal Robots in Education (IPRE)‏ Sec 9-7 Web Design.
COMPUTATION WITH STRINGS 1 DAY 2 - 8/27/14 LING 3820 & 6820 Natural Language Processing Harry Howard Tulane University.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
More Strings CS303E: Elements of Computers and Programming.
Input and Output CMSC 120: Visualizing Information Lecture 4/10.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Data Manipulation Variables, Data Types & Math. Aug Variables A variable is a name (identifier) that points to a value. They are useful to store.
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.
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.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Input, Output and Variables GCSE Computer Science – Python.
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
Introduction to Programming
Generating Random Numbers
Tuples and Lists.
An Introduction to Python and Its Use in Bioinformatics
Variables, Expressions, and IO
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Strings
Introduction to Strings
Creation, Traversal, Insertion and Removal
Chapter 8 More on Strings and Special Methods
Chapter 8 More on Strings and Special Methods
Data types Numeric types Sequence types float int bool list str
8 – Lists and tuples John R. Woodward.
T. Jumana Abu Shmais – AOU - Riyadh
Chapter 8 More on Strings and Special Methods
Introduction to Programming
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Variables, Data Types & Math
Variables, Data Types & Math
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
15-110: Principles of Computing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Computer Science
Variables, Data Types & Math
Introduction to Strings
Python Strings.
By Himanshi dixit 11th ’B’
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Class 8 setCoords Oval move Line cloning vs copying getMouse, getKey
Presentation transcript:

Strings CS303E: Elements of Computers and Programming

The String Data Type Represents text Represents text So far, we’ve used it for input and output So far, we’ve used it for input and output Now, we’ll take a closer look: Now, we’ll take a closer look: –Representation –String operations Like arithmetic operations, but different Like arithmetic operations, but different

String Representation Defined with single or double quotes Defined with single or double quotes –Need to start and end with same type of quote firstName=‘Steve’ firstName=‘Steve’ lastName=“Jobs” lastName=“Jobs” Represented as a sequence of characters Represented as a sequence of characters Steve

Strings as Input Get using raw_input() Get using raw_input() OR input(), but then the user should include the text in quotation marks OR input(), but then the user should include the text in quotation marks Best to use raw_input() and not expect the user to know to use quotation marks Best to use raw_input() and not expect the user to know to use quotation marks

String Concatenation “Glue” two strings together to form a new string using + “Glue” two strings together to form a new string using +Example: myString=“call ”+”911” print myString Output: call 911

String Concatenation + is overloaded in Python + is overloaded in Python –Math and strings Determines whether to add or concatenate based on the first operand Determines whether to add or concatenate based on the first operand – + … indicates math – + … indicates concatenation –canNOT combine the two

String Concatenation But what if you want to combine the two? But what if you want to combine the two? –Convert numbers to strings using str() To convert a string to a number: To convert a string to a number: –Use int(), float(), etc –Or eval(), which evaluates an expression in a string and converts it to a number

String Repetition Use * to repeat a string any number of times Use * to repeat a string any number of times –Concatenate a string with itself Example: print “Hello”*3 Output: HelloHelloHello * is also overloaded * is also overloaded

String Membership You can test to see if a string is a member of a larger string You can test to see if a string is a member of a larger stringExample: myString = “Hello” print “el” in myString Output:True

Question: String Concatenation Which is an invalid expression? Which is an invalid expression? A B. “a” + “b” C. “7” + “car” D. “hello” + 4

String Length Recall that strings are sequences of characters Recall that strings are sequences of characters Use len() to count those characters Use len() to count those characters –Returns the length of the string –Includes all characters---even the spaces

String Length Example: myString=“Hello, World” print “The length of “ + myString + “ is ” + len(myString) Output: The length of Hello, World is 12

String Indexing Use indexing to access the individual characters in a string Use indexing to access the individual characters in a string Characters are numbered, or indexed, beginning at 0 Characters are numbered, or indexed, beginning at 0Example: Length is 11. Index values are 0-10 helloworld

String Indexing A string of length n has characters with index values of 0 to n-1 A string of length n has characters with index values of 0 to n-1 Syntax: Syntax:<stringName>[index]

Indexing: Negative Offsets Negative offset: count backwards from end of string Negative offset: count backwards from end of string >>> myString = “help” >>> myString[0] ‘h’ >>> myString[-1] #first character from end ‘p’ >>> myString[-2] # second character from end ‘l’

String Indexing Example:myString=“hello” hello 01234

Strings are immutable---you cannot change them. Strings are immutable---you cannot change them. This will NOT work: myString=“help” myString=“help” myString[0]=“k” myString[0]=“k”

Question: String Indexing In the string, “Monday”, what is the index of ‘d’? In the string, “Monday”, what is the index of ‘d’? A. 3 B. 4 C. 5

Strings and for Loops Once we can index into a string, we can use a for loop to iterate through each character: Once we can index into a string, we can use a for loop to iterate through each character:myString=“hello” for i in range(len(myString)) print myString[i] print myString[i]

Strings and for Loops: Exercise Print the characters in a string in reverse Print the characters in a string in reverse

Iterating Over a String: Take Two Again, strings are sequences of characters Again, strings are sequences of characters So we can iterate… So we can iterate…Example:myString=“hello” for ch in myString print ch Output:hello hello

String Examples >>> myString = “Help, “ >>> myString += “I got a spam burger for dinner.” >>> print myString Help, I got a spam burger for dinner. What is the output? >>> print “-” * 80 >>> myString = “hi world hello” >>> print “hello” in myString True

String Examples >>> myString = “411” >>> for c in myString: print c print c411

Using eval(): Examples >>> numString = “400” >>> eval(numString) 400 >>> numString = “3+5” >>> eval(numString) 8

Exercises Write a function that takes a string argument, and prints the first character in the string (assume the string has at least one character). Write a function that takes a string argument, and prints the first character in the string (assume the string has at least one character). Write a function that takes a string argument, and prints every other character, starting with the first. Write a function that takes a string argument, and prints every other character, starting with the first.