Ex:'Martin Luther King Jr.'.split()splits the string literal "Martin Luther King Jr." using any whitespace character as thedefault separator and returns the list of tokens ['Martin', 'Luther', 'King', 'Jr.'].The separator can be changed by calling split() with a string argument. Ex:'a#b#c'.split('#')uses the "#" separator to split thestring "a#b#c" into the three tokens ['a', 'b', 'c'].PARTICIPATIONACTIVITY2.11.1: Splitting a string into tokens.Fi2 11 1 St ilitlAnimation content:undefinedAnimation captions:1. Original string contains a pathname to an mp3 of your favorite song.2. The pathname is split using the delimiter ' / '.3. The 3 tokens are assigned to the variable my_tokens as a list of strings.4. When split() is called with no argument, the delimiter defaults to a space character.234©zyBooks 01/11/22 11:22 1168648Karen GarciaSNHUIT140v3©zyBooks 01/11/22 11:22 1168648Karen GarciaSNHUIT140v3
Figure 2.11.1: String split example.url=input('Enter URL:\n')tokens=url.split('/')# Uses '/'separatorprint(tokens)Enter URL:['http:', '', 'en.wikipedia.org', 'wiki','Lucille_ball']...Enter URL: en.wikipedia.org/wiki/ethernet/['en.wikipedia.org', 'wiki', 'ethernet', '']The example above shows how split() might be used to ±nd the elements of a path to a web page; the separator used is the forwardslash character '/'. The split() method creates a new list, ordered from left to right, containing a new string for each sequence ofcharacters located between '/' separators. Thus the URL is split into ['http:', '', 'en.wikipedia.org','wiki', 'Lucille_ball']. The separator character is not included in the resulting strings.If the split string starts or ends with the separator, or if two consecutive separators exist, then the resulting list will contain an emptystring for each such occurrence. Ex: The consecutive forward slashes of 'http://' and the ending forward slash of '.../wiki/ethernet/'generate empty strings. If the separator argument is omitted from split(), thus splitting the string wherever whitespace occurs, then noempty strings are generated.PARTICIPATIONACTIVITY2.11.2: String split() method.zyDE 2.11.1: More string splitting.Run the following program and observe the output. Edit the program by changing the split() method separator to "//"and observe the output.Load default template...file='C:/Users/Charles Xavier//Documenseparator='/'results=file.split(separator)print('Separator ({}):'.format(separatorRun123456©zyBooks 01/11/22 11:22 1168648Karen GarciaSNHUIT140v3©zyBooks 01/11/22 11:22 1168648Karen GarciaSNHUIT140v3
Use the variable song to answer the questions below.song="I scream; you scream; we all scream, for ice cream.\n"1)What is the result ofsong.split()?2)What is the result ofsong.split('\n')?3)What is the result ofsong.split('scream')?The join() methodThejoin()string method performs the inverse operation of split() by joining a list of strings together to create a single string. Ex:my_str = '@'.join(['billgates', 'microsoft'])assigns the string '[email protected]' to my_str. The separator '@'provides a join() method that accepts a single list argument. Each element in the list, from left to right, is concatenated to create a newstring object with the separator placed between each list element. The separator can be any string, including multiple characters or anempty string.
Upload your study docs or become a
Course Hero member to access this document
Upload your study docs or become a
Course Hero member to access this document
End of preview. Want to read all 68 pages?
Upload your study docs or become a
Course Hero member to access this document
Term
Winter
Professor
N/A
Tags
String literal, Karen Garcia