Download presentation
Presentation is loading. Please wait.
Published byKrista Lehtilä Modified over 5 years ago
1
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
2
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
3
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˽˽ \t” [“CS”, “ ”]
4
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”]
5
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”, “”]
6
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)!
7
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.
8
“ “ 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!
9
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)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.