DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

String in Python (9)

Buy Me a Coffee

*Memos:

splitlines() can split a string at one or more line boundaries as shown below:

*Memos:

  • The 1st argument is keepends(Optional-Default:False-Type:bool). *If keepends is True, one or more line boundaries are included otherwise they aren't included.
  • These below are line boundaries:
\n Line Feed
\r Carriage Return
\r\n Carriage Return + Line Feed
\v or \x0b Line Tabulation
\f or \x0c Form Feed
\x1c File Separator
\x1d Group Separator
\x1e Record Separator
\x85 Next Line (C1 Control Code)
\u2028 Line Separator
\u2029 Paragraph Separator
v1 = '1 one\n2 two\r3 three\r\n4 four\v5 five' v2 = '1 one\x0b2 two\f3 three\x0c4 four' v3 = '1 one\x1c2 two\x1d3 three\x1e4 four' v4 = '1 one\x852 two\u20283 three\u20294 four' print(v1) # 1 one # 3 three # 4 four5 five  print(v1.splitlines()) print(v1.splitlines(keepends=False)) # ['1 one', '2 two', '3 three', '4 four', '5 five']  print(v1.splitlines(keepends=True)) # ['1 one\n', '2 two\r', '3 three\r\n', '4 four\x0b', '5 five']  print(v2) # 1 one2 two3 three4 four  print(v2.splitlines()) # ['1 one', '2 two', '3 three', '4 four']  print(v2.splitlines(keepends=True)) # ['1 one\x0b', '2 two\x0c', '3 three\x0c', '4 four']  print(v3) # 1 one2 two3 three4 four  print(v3.splitlines()) # ['1 one', '2 two', '3 three', '4 four']  print(v3.splitlines(keepends=True)) # ['1 one\x1c', '2 two\x1d', '3 three\x1e', '4 four']  print(v4) # 1 one…2 two
3 three
4 four  print(v4.splitlines()) # ['1 one', '2 two', '3 three', '4 four']  print(v4.splitlines(keepends=True)) # ['1 one\x85', '2 two\u2028', '3 three\u2029', '4 four'] 
Enter fullscreen mode Exit fullscreen mode
v = '1 one 2 two 3 three 4 four 5 five' print(v) # 1 one 2 two 3 three 4 four 5 five  print(v.splitlines()) print(v.splitlines(keepends=True)) # ['1 one 2 two 3 three 4 four 5 five'] 
Enter fullscreen mode Exit fullscreen mode
v = '' print(v.splitlines()) # [] 
Enter fullscreen mode Exit fullscreen mode

partition() can split a string at the 1st occurrence of a separator, searching from the left to the right as shown below:

*Memos:

  • The 1st argument is sep(Required-Type:str): *Memos:
    • It's the separator of the one or more characters to separate a string.
    • An empty string cannot be set.
    • Don't use sep=.
  • It returns a tuple of 3 elements.
  • If sep isn't found, a tuple of the string itself and two empty strings in order is returned as 3 elements.
v = "It's very very good" print(v.partition('er')) # ("It's v", 'er', 'y very good')  print(v.partition('very')) # ("It's ", 'very', ' very good')  # ↓ print(v.partition(' ')) # ("It's", ' ', 'very very good')  # ↓↓ print(v.partition(' ')) print(v.partition('ER')) print(v.partition('VERY')) print(v.partition('really')) # ("It's very very good", '', '')  print(v.partition('')) # ValueError: empty separator 
Enter fullscreen mode Exit fullscreen mode
v = '' print(v.partition('er')) # ('', '', '') 
Enter fullscreen mode Exit fullscreen mode

rpartition() can split a string at the 1st occurrence of a separator, searching from the right to the left as shown below:

*Memos:

  • The 1st argument is sep(Required-Type:str): *Memos:
    • It's the separator of the one or more characters to separate a string.
    • An empty string cannot be set.
    • Don't use sep=.
  • It returns a tuple of 3 elements.
  • If sep isn't found, a tuple of two empty strings and the string itself in order is returned as 3 elements.
v = "It's very very good" print(v.rpartition('er')) # ("It's very v", 'er', 'y good')  print(v.rpartition('very')) # ("It's very ", 'very', ' good')  # ↓ print(v.rpartition(' ')) # ("It's very very", ' ', 'good')  # ↓↓ print(v.rpartition(' ')) print(v.rpartition('ER')) print(v.rpartition('VERY')) print(v.rpartition('really')) # ('', '', "It's very very good")  print(v.rpartition('')) # ValueError: empty separator 
Enter fullscreen mode Exit fullscreen mode
v = '' print(v.rpartition('er')) # ('', '', '') 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)