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.

Slides:



Advertisements
Similar presentations
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Advertisements

Computer Science & Engineering 2111 Text Functions 1CSE 2111 Lecture-Text Functions.
Ch 8. Characters and Strings Timothy Budd 2 Characters and Literals Strings Char in C++ is normally an 8-bit quantity, whereas in Java it is a 16-bit.
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.
Lists Introduction to Computing Science and Programming I.
Strings Victor Norman CS104 Calvin College. Reading Quiz Counts toward your grade.
Files in Python Input techniques. Input from a file The type of data you will get from a file is always string or a list of strings. There are two ways.
Geography 465 Assignments, Conditionals, and Loops.
Identifiers and Assignment Statements. Data structures In any programming language you need to refer to data The simplest way is with the actual data.
1.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 7 Strings, Lists.
Lists in Python.
Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
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.
Regular Expressions.
Built-in Data Structures in Python An Introduction.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
Overview A regular expression defines a search pattern for strings. Regular expressions can be used to search, edit and manipulate text. The pattern defined.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
Exam Prep and Wrap-Up Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
More Strings CS303E: Elements of Computers and Programming.
Python Let’s get started!.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
INLS 560 – S TRINGS Instructor: Jason Carter. T YPES int list string.
8 1 String Manipulation CGI/Perl Programming By Diane Zak.
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.
Last of String Manipulation. Programming Challenge: Usernames Let’s say you work at the IT department for NYU and you are asked to generate student ID’s.
Chapter 23 The String Section (String Manipulation) Clearly Visual Basic: Programming with Visual Basic nd Edition.
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.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
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.
String and Lists Dr. José M. Reyes Álamo.
String Processing Upsorn Praphamontripong CS 1110
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Strings Part 1 Taken from notes by Dr. Neil Moore
Chapter 12: Text Processing
Winter 2018 CISC101 11/16/2018 CISC101 Reminders
Chapter 12: Text Processing
String Manipulation Part 2
Using files Taken from notes by Dr. Neil Moore
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Strings and Lists – the split method
String Manipulation Chapter 7 Attaway MATLAB 4E.
CSC 221: Introduction to Programming Fall 2018
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Text Analyzer BIS1523 – Lecture 14.
Representation and Manipulation
Basic String Operations
CS 1111 Introduction to Programming Spring 2019
Topics Basic String Operations String Slicing
Introduction to Computer Science
String methods 26-Apr-19.
IST256 : Applications Programming for Information Systems
Text Manipulation Chapter 7 Attaway MATLAB 5E.
Topics Basic String Operations String Slicing
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Topics Basic String Operations String Slicing
Introduction to Computer Science
Presentation transcript:

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 notation (like in graphics) Note that in the methods mentioned which return string results, they create a new string; they do NOT change the old one! upper(), lower() return a new string with the alphabetic characters in a string changed to the other case, they don’t change any other characters at all replace(old, new) will return a brand new string with the first pattern, “old”, replaced by the second pattern, “new”, wherever it is in the original string

Strings are immutable Strings are immutable – meaning they cannot be changed once they are created They can be discarded; the variable name can be reassigned to another different string But individual characters cannot be changed in a string If name = “Mary” you cannot say name[0]= “S” To change the first letter of the string you would have to do something like name = ‘S’ + name[1:] To make a string all upper case, you would say something like name = name.upper() - that is, upper returns a string and you assign that new string to the same variable, the old string is discarded

Strings and whitespace whitespace is a specific name for a set of characters, namely the blank or space character ‘ ‘, the tab character ‘\t’, and the newline character ‘\n’ They are very often used as delimiters (markers between) in various string methods (strip, split) str.strip() removes all leading and trailing whitespace characters from a string. It does NOT alter any whitespace characters in the middle of the string. Example: “ ab\tcd\n”.strip() will “ab\tcd” str.split() will use whitespace characters as delimiters to break the string into a list of strings (see a following slide)

The find method, the index method, the count method and the in operator All of these methods and operators involve finding something in a string They could all be written with loops and if statements but it’s much easier to use prewritten functions in is the simplest – syntax a in b It returns a Bool if it finds the first string a, somewhere in the string b. It does not matter if there are multiple occurrences of the string a, does not matter where the string a is positioned in the string b. It does have to be an exact match, as far as case, spacing, etc.

The find method and index method find(target) – this is called with the dot notation on the source string, so a call like s.find(“me”) is looking in the string s for the string “me” It works from the left end of the string every time It returns the location of the first occurrence of the target string It returns -1 if the target string does not occur at all in the source The index method works the same as the find method, except that if the search fails, the index method causes an exception and the program crashes! Safety rule: use the “in” operator first, before using the index method

The count method The count method is similar to the find and index methods count(“m”) will return an integer from 0 up to length of the string It is a method so it’s called with the dot notation s.count(“m”) would return an integer that shows how many m’s appear in the string

Summary of location/membership the in operator gives you just True or False, tells you the target string is in the source string or not, NO location find and index return the location of the left-most occurrence of the target string in the source string (find returns -1 for failure, index crashes the program for failure) count returns the number of occurrences of the target string in the source string