FAQ: String Methods - Splitting Strings III

Study the post near the top that shows what the split list looks like. That should help.

FAQ: String Methods - Splitting Strings III - #5 by levieo

What is the example of using /t in split? and when we need to use it

Thinking about this lesson here and the questions being asked raised an interesting point about the split method in Python. The lesson itself provided an example code and output from Python:

smooth_chorus = \ """And if you said, "This life ain't good enough." I would give my world to lift you up I could change my life to better suit your mood Because you're so smooth""" chorus_lines = smooth_chorus.split('\n') print(chorus_lines)

Based on the output the assumptions I am making are that:

  1. Split() on string defaults to creating a list with single quotation marks around the elements within the list. This covers the case where the string contains double quotation marks.
  2. However, in the case that there are single quotation marks within the string being split, it will use double quotation marks instead.
  3. It will then employ the escape character if the string being “converted” into the list element has both single and double quotation marks, since it has to use one of them to “enclose” the string element within the list.

I couldn’t find documentation on this directly so would appreciate any comments about this hypothesis.