Strings. A string is a series of characters Characters can be referenced by using brackets The first character is at position 0 mystring = “the” letter.

Slides:



Advertisements
Similar presentations
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
Advertisements

Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Java Programming Strings Chapter 7.
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.
Chapter 4 Working with Strings. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Sequence of characters.
Programming 2 CS112- Lab 2 Java
ECS15 for and sequence. Comments  Another way to repeat.
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Guide to Programming with Python
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Python Programming Chapter 7: Strings Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
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.
Programming in Python Part III Dr. Fatma Cemile Serçe Atılım University
Strings CS303E: Elements of Computers and Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Arrays Character Arrays and Strings Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
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:
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
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
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.
Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from.
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.
Strings in Python String Methods. String methods You do not have to include the string library to use these! Since strings are objects, you use the dot.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
String and Lists Dr. José M. Reyes Álamo.
Tuples and Lists.
String Processing Upsorn Praphamontripong CS 1110
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Strings Part 1 Taken from notes by Dr. Neil Moore
CHAPTER 7 STRINGS.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS 106A, Lecture 9 Problem-Solving with Strings
Introduction to Strings
Introduction to Strings
Chapter 8 More on Strings and Special Methods
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
16 Strings.
Basic String Operations
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
String.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Basic String Operations String Slicing
Bryan Burlingame 13 March 2019
String Processing 1 MIS 3406 Department of MIS Fox School of Business
Introduction to Strings
Python Review
Python Strings.
Topics Basic String Operations String Slicing
By Himanshi dixit 11th ’B’
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Topics Basic String Operations String Slicing
Introduction to Computer Science
Presentation transcript:

Strings

A string is a series of characters Characters can be referenced by using brackets The first character is at position 0 mystring = “the” letter = mystring[2] #letter becomes ‘e’ t mystring he 012

length The len function returns the length of a string mystring=“bob” len(mystring) #3 len(“adam”) #4 length=len(mystring) last = mystring[len-1] #retrieves last char

for loops mystring = "CS is cool!" for c in mystring: print c index=0 while index < len(mystring): print mystring[index] index += 1

Exercises 1.Write a for or while loop to print a string backwards.

Slices Select a segment of a string Specify [start:end] –include start but do not include end –if you do not specify start slice starts from the beginning –if you do not specify end slices goes to end mystring=“CS is cool” print mystring[6:10] print mystring[2:7] print mystring[:4] print mystring[:]

String Comparison/in == tests to see if strings are the same >, < compares strings alphabetically The in operator tests whether a given character appears in a given string –‘c’ in “chocolate” #true –‘z’ in “chocolate” #false

Immutability Strings are immutable –they cannot be changed

string module Contains useful methods for strings Dot notation allows us to call a method on a string object import string mystring=“adam” string.find(mystring, “a”) #returns index of first instance found mystring=“CS is cool” mystring.split() #result [‘CS’,’is’,’cool’] newstring = mystring.replace(“CS”, “Econ”)

Exercises 1.Write a program that prompts the user for two strings and determines the number of two-character sequences that appear in both the first and second strings. Exclude spaces in your comparison. “CS is cool” “the old cow” would have two matches “co” for “cool” and “cow” and “ol” for “cool” and “old”