Strings Part 2 Taken from notes by Dr. Neil Moore & Dr. Debby Keen

Slides:



Advertisements
Similar presentations
Files in Python The Basics. Why use Files? Very small amounts of data – just hardcode them into the program A few pieces of data – ask the user to input.
Advertisements

Files in Python Caution about readlines vs. read and split.
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.
CATHERINE AND ANNIE Python: Part 4. Strings  Strings are interesting creatures. Although words are strings, anything contained within a set of quotes.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Methods OR HOW TO MAKE A BIG PROGRAM SEEM SMALLER.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
CS 115 Lecture 17 2-D Lists Taken from notes by Dr. Neil Moore.
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.
Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:
Prof. Katherine Gibson Prof. Jeremy Dixon
String and Lists Dr. José M. Reyes Álamo.
List Algorithms Taken from notes by Dr. Neil Moore & Dr. Debby Keen
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
Formatting Output.
Tuples and Lists.
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Quiz 11/15/16 – C functions, arrays and strings
CS 115 Lecture 8 Structured Programming; for loops
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Formatting Output.
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
2-D Lists Taken from notes by Dr. Neil Moore
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Strings Part 1 Taken from notes by Dr. Neil Moore
Exceptions and files Taken from notes by Dr. Neil Moore
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Boolean logic Taken from notes by Dr. Neil Moore
Random numbers 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
Strings Part 2 Taken from notes by Dr. Neil Moore
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
List Algorithms Taken from notes by Dr. Neil Moore
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Using files Taken from notes by Dr. Neil Moore
Exceptions and files Taken from notes by Dr. Neil Moore
Passing Parameters by value
Strings and Lists – the split method
Fundamentals of Data Structures
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Structured Programming Taken from notes by Dr. Neil Moore
String and Lists Dr. José M. Reyes Álamo.
Debugging Taken from notes by Dr. Neil Moore
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Last Class We Covered Escape sequences File I/O Uses a backslash (\)
Data Manipulation & Regex
Notes about Homework #4 Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming:
Methods – on strings and other things
Debugging Taken from notes by Dr. Neil Moore
Recursion Taken from notes by Dr. Neil Moore
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CISC101 Reminders All assignments are now posted.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Boolean logic Taken from notes by Dr. Neil Moore
For loops Taken from notes by Dr. Neil Moore
JavaScript: Objects.
String methods 26-Apr-19.
Text Manipulation Chapter 7 Attaway MATLAB 5E.
Python Strings.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CSE 231 Lab 6.
2-D Lists Taken from notes by Dr. Neil Moore
Functions Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Introduction to Computer Science
Presentation transcript:

Strings Part 2 Taken from notes by Dr. Neil Moore & Dr. Debby Keen CS 115 Lecture 14 Strings Part 2 Taken from notes by Dr. Neil Moore & Dr. Debby Keen

Strings to lists to strings There are two string methods which work with lists of strings split splits a string into words or other parts And returns a list of strings join takes a list of strings and combines them And returns a single string

Splitting strings The split method breaks a string apart and returns a list of the pieces (smaller strings). There are two ways to call split. No arguments: name.split() Splits the string on sequences of whitespace Gives you a list of “words” phrase = “attention CS 115 students” words = phrase.split() [“attention”, “CS”, “115”, “students”] Multiple whitespaces in a row are skipped, as is leading or trailing whitespace phrase = “˽CS˽˽115-001\t” [“CS”, “115-001”]

Splitting with a delimiter You can also pass an arbitrary delimiter (separator) as an argument to split. It will break the string apart on that delimiter: date = “04/08/2015” parts = date.split(“/”) gives[“04”, “08”,”2015”]

Splitting with a delimiter There are a few differences from splitting on whitespace Multiple delimiters in a row are NOT combined into one. Instead you get an empty string in the resulting list: parts = “A,,B,C”.split(“,”) gives [“A”,””,”B”,”C”] Delimiters at the beginning/end also give empty strings in the resulting list: parts = “:A:2:”.split(“:”) gives [“”, “A”, “2”, “”]

A note about split Something people don’t notice often Whatever delimiter you use for splitting, whether whitespace or a given delimiter The resulting list of strings contains NONE of the delimiting characters! Sounds obvious but people seem to forget that So if you split on whitespace, the strings in the list have NO whitespace in them If you split on a “:”, there will be NO colons in the resulting list of strings People write things like this: MyInp.strip().split() Why is this silly? The strip takes off whitespace on the ends, then the split happens and ALL whitespace goes away. The strip is superfluous (redundant)!

Difference between “ “ and whitespace People think that name.split() and name.split(“ “) are “the same”. They are NOT! Giving NO argument as the first example means to use all kinds of whitespace as the delimiter(s). Tabs, newlines, ANY number of spaces become delimiters. If you use an argument to split, like “ “, you mean “exactly this character and no others” is the delimiter.

“ “ and whitespace Example: my_str = “˽˽abc˽d\n˽˽e\tfg˽” my_str.split() gives you [“abc”,”d”,”e”,”fg”] all traces of whitespace characters gone my_str.split(“ “) gives you [“”,””,”abc”,”d\n”, “”, “e\tfg”,””] Many MORE elements AND lots of them are empty strings, AND all other whitespace characters like \t and \n are still there!

Joining strings together What if we want to do the opposite of split? That is, take a list of strings … … and join them together with a delimiter First, let’s write the code to do this by hand: join.py Python has a built-in method to do this: join But calling it looks a little funny result = “-”.join(parts) The delimiter not the list, comes before the dot! We ask the delimiter to join the list of strings together parts is a sequence of strings (usually a list of strings)