r/PythonLearning • • 17m ago

Help Request where did the '\n' go?

using f.read().split() removes the \n escape sequence, why does that happen?

1 Upvotes

1 comment sorted by

2

u/Confident_Rod_9717 10m ago

From the built in docs, it seems unless you specify a separator, it splits on any whitespace character, not just space

>>> help(str.split)
Help on method descriptor split:

split(self, /, sep=None, maxsplit=-1) unbound builtins.str method
   Return a list of the substrings in the string, using sep as the separator string.

     sep
       The separator used to split the string.

       When set to None (the default value), will split on any
       whitespace character (including \n \r \t \f and spaces) and
       will discard empty strings from the result.
     maxsplit
       Maximum number of splits.
       -1 (the default value) means no limit.

   Splitting starts at the front of the string and works to the end.

   Note, str.split() is mainly useful for data that has been
   intentionally delimited.  With natural text that includes
   punctuation, consider using the regular expression module.