ZhangZhihui's Blog  

str.split() and str.partition() both split strings, but they behave very differently and are used for different purposes.

Here’s the clearest comparison:


1. Return Types

str.split()

Returns a list of all split pieces.

"a.b.c".split(".")
# ['a', 'b', 'c']

str.partition()

Returns a tuple of exactly 3 elements:

(before, separator, after)

 

"a.b.c".partition(".")
# ('a', '.', 'b.c')

 


2. Number of splits

split()

Splits all occurrences (unless you specify maxsplit).

partition()

Splits only the first occurrence of the separator.


3. Separator handling

split()

The separator is removed.

"a.b".split(".")
# ['a', 'b']

partition()

The separator is kept in the middle tuple element.

"a.b".partition(".")
# ('a', '.', 'b')

 


4. Behavior when separator is NOT found

split()

Returns the original string in a 1-element list:

"a".split(".")
# ['a']

partition()

Returns:

(original, '', '')

Example:

"a".partition(".")
# ('a', '', '')

 


5. When to use each

✔ Use split() when:

  • You want multiple pieces

  • You want a list of tokens

  • You don’t care about the separator

Example: parsing CSV-like text, splitting paths, etc.


✔ Use partition() when:

  • You want to split once at the first occurrence

  • You want both the part before and after

  • You want to preserve the separator

  • You want predictable 3-tuple output

This makes partition() ideal for:

  • extracting the first sentence

  • splitting key-value pairs once

  • cutting header/body in text

Example you used:

__doc__.partition(".")[0]

This safely extracts the first sentence.


🧠 Quick Summary

Featuresplit()partition()
Output list 3-tuple
Number of splits all first only
Keeps separator no yes
If separator missing returns list of 1 returns (string, '', '')

If you want, I can show examples of rsplit(), rpartition(), splitlines(), etc.

 
You said:
Please show me examples of splitlines().
 
ChatGPT said:

Here are clear, practical examples showing how str.splitlines() works and how it differs from other split methods.


Basic behavior

splitlines() splits a string at line boundaries (\n, \r, \r\n, etc.).

Example 1 — basic newline splitting

text = "hello\nworld\npython"
text.splitlines()

Output:

['hello', 'world', 'python']

It handles many newline types transparently

Example 2 — mixing \n, \r\n, and \r

text = "a\nb\r\nc\rd"
text.splitlines()

Output:

['a', 'b', 'c', 'd']

You don’t have to care which OS the text came from — it “just works.”


Keep the line endings using keepends=True

Example 3 — preserving newline characters

text = "hello\nworld\n"
text.splitlines(keepends=True)

Output:

['hello\n', 'world\n']

Notice the trailing newline is kept with the last element.


Trailing newline behavior

Example 4 — trailing newline disappears if keepends=False (default)

"one\ntwo\n".splitlines()

Output:

['one', 'two']

Example 5 — with keepends=True

"one\ntwo\n".splitlines(True)

Output:

['one\n', 'two\n']

Empty lines are respected

Example 6 — blank line between lines

text = "a\n\nb"
text.splitlines()

Output:

['a', '', 'b']

String without newline returns original string in a list

Example 7

"hello".splitlines()

Output:

['hello']

📚 Quick Summary

Featuresplitlines()
Splits on All linebreak types (\n, \r\n, \r)
Preserves linebreaks Only if keepends=True
Empty lines preserved Yes
Consistent return type Always a list

 

posted on 2025-12-10 10:36  ZhangZhihuiAAA  阅读(8)  评论(0)    收藏  举报