INLS 560 – S TRINGS Instructor: Jason Carter. T YPES int list string.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

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.
INLS 560 – D ICTIONARIES Instructor: Jason Carter.
Lists Introduction to Computing Science and Programming I.
Strings, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
INLS 560 – V ARIABLES, E XPRESSIONS, AND S TATEMENTS Instructor: Jason Carter.
Lists in Python.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Python for Informatics: Exploring Information
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Strings CS303E: Elements of Computers and Programming.
INLS 560 – C ONDITIONALS Instructor: Jason Carter.
Fundamentals of Python: First Programs
CIT 590 Intro to Programming Lecture 5 – completing lists.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Chapter 7: Characters, Strings, and the StringBuilder.
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.
More Strings CS303E: Elements of Computers and Programming.
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.
CIT 590 Intro to Programming Lecture 4. How are assignments evaluated Pay attention to what the HW says it is trying to teach you about ‘modular programming’
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
CSC 4630 Meeting 17 March 21, Exam/Quiz Schedule Due to ice, travel, research and other commitments that we all have: –Quiz 2, scheduled for Monday.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
Computer Science 101 Python Lists. Literals, Assignment, Comparisons, Concatenation Similar to the behavior of strings so far a = [1, 2, 3] b = range(1,
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
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.
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.
Computer Programming ||
String Processing Upsorn Praphamontripong CS 1110
Basic operators - strings
Strings Part 1 Taken from notes by Dr. Neil Moore
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Strings
Introduction to Strings
MSIS 655 Advanced Business Applications Programming
Python - Strings.
4. sequence data type Rocky K. C. Chang 16 September 2018
String and Lists Dr. José M. Reyes Álamo.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Sequences Introduction to Lists List Slicing
16 Strings.
Fundamentals of Python: First Programs
Basic String Operations
Introduction to Strings
Microsoft Visual Basic 2005: Reloaded Second Edition
Intro to Computer Science CS1510 Dr. Sarah Diesburg
15-110: Principles of Computing
Topics Sequences Lists Copying Lists Processing Lists
Topics Basic String Operations String Slicing
Introduction to Computer Science
String methods 26-Apr-19.
Topics Sequences Introduction to Lists List Slicing
Introduction to Strings
Python Strings.
Topics Basic String Operations String Slicing
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
Presentation transcript:

INLS 560 – S TRINGS Instructor: Jason Carter

T YPES int list string

B ASIC S TRING O PERATIONS Many types of programs perform operations on strings So far we’ve only really seen strings as input/output In Python, many tools for examining and manipulating strings Strings are sequences, so many of the tools that work with sequences work with strings

A CCESSING THE I NDIVIDUAL C HARACTER IN A S TRING To access an individual character in a string: Use a for loop Format: for character in string: Useful when need to iterate over the whole string, such as to count the occurrences of a specific character Use indexing Each character has an index specifying its position in the string, starting at 0 Format: character = my_string[i]

A CCESSING C HARACTERS IndexError exception will occur if: You try to use an index that is out of range for the string Likely to happen when loop iterates beyond the end of the string len(string) function can be used to obtain the length of a string Useful to prevent loops from iterating beyond the end of a string myString = “Hello World” n = len(myString) print(myString[n+1]) #This will cause an IndexError print(myString[n]) #This will also cause an IndexError

A CCESSING C HARARACTERS “C”“o”“m”“p”“u”“t”“e”“r” my_string = “Computer” print my_string[0]

U SING A F OR L OOP TO A CCESS C HARACTERS

S TRING C ONCATENATION Concatenation: appending one string to the end of another string Use the + operator to produce a string that is a combination of its operands The augmented assignment operator += can also be used to concatenate strings The operand on the left side of the += operator must be an existing variable; otherwise, an exception is raised

S TRING C ONCATENATION Unlike print, string concatenation does not put spaces between your strings. s1 = “INLS560” s2 = “rocks!” bigstring = s1 + “ “ + s2 print(bigstring) #prints INLS560 rocks!

S TRINGS A RE I MMUTABLE Strings are immutable (unchangeable) Once they are created, they cannot be changed Concatenation doesn’t actually change the existing string, but rather creates a new string and assigns the new string to the previously used variable Cannot use an expression of the form string[index] = new_character Statement of this type will raise an exception

S TRING F UNCTIONS Strings in Python have many types of methods, divided into different types of operations General format: mystring.function(arguments) Some functions test a string for specific characteristics Generally Boolean methods, that return True if a condition exists, and False otherwise

S TRING T ESTING F UNCTIONS

P RACTICE Write a program that asks the user to input a sentence and counts the number of spaces that are in the inputted string. Example: Please enter a sentence: Fall break was so fun! The number of spaces in your sentence is: 4

S TRING M ODIFICATION M ETHODS

M ORE S TRING M ETHODS Programs commonly need to search for substrings Several methods to accomplish this: endswith( substring ): checks if the string ends with substring Returns True or False startswith( substring ): checks if the string starts with substring Returns True or False

M ORE S TRING M ETHODS Several methods to accomplish this cont’d): find( substring ): searches for substring within the string Returns lowest index of the substring, or if the substring is not contained in the string, returns -1 replace( substring, new_string ): Returns a copy of the string where every occurrence of substring is replaced with new_string

T ESTING, S EARCHING, AND M ANIPULATING S TRINGS You can use the in operator to determine whether one string is contained in another string General format: string1 in string2 string1 and string2 can be string literals or variables referencing strings Similarly you can use the not in operator to determine whether one string is not contained in another string

S PLITTING A S TRING split function: returns a list containing the words in the string By default, uses space as separator Can specify a different separator by passing it as an argument to the split method

S PLITTING A S TRING

S PLITTING A S TRING - O UTPUT