A quick reference to Python string methods - Splitting and Slicing

Kelvin Wangonya - Sep 13 '19 - - Dev Community

Note: * represents a required parameter.

Splitting and Slicing

join()

Parameters

string.join(iterable)
Enter fullscreen mode Exit fullscreen mode

iterable*: a python iterable e.g a list, tuple, string, dictionary or set.

Return value

Returns a string concatenated with the elements of the passed in iterable.

Example

Python 3.7.4

>>> n1 = ['1', '2', '3', '4', '5']
>>> n2 = ('1', '2', '3', '4', '5')
>>> separator = ', '

>>> separator.join(n1)
'1, 2, 3, 4, 5'
>>> separator.join(n2)
'1, 2, 3, 4, 5'
Enter fullscreen mode Exit fullscreen mode

partition()

Parameters

string.partition(separator)
Enter fullscreen mode Exit fullscreen mode

separator*: the part of the string that will trigger the separation. If it occurs more than once in the string, the first occurrence is considered.

Return value

Return a tuple containing three parts:

  • the part of the string before the separator
  • the separator itself
  • the part of the string after the separator

Example

Python 3.7.4

>>> s = "I feel the need - the need for speed!"
>>> s.partition("-")
('I feel the need ', '-', ' the need for speed!')

>>> s.partition(",")
('I feel the need - the need for speed!', '', '')

>>> s = "I feel the need - the need - for speed!"
>>> s.partition("-")
('I feel the need ', '-', ' the need - for speed!')
Enter fullscreen mode Exit fullscreen mode

rpartition()

Parameters

string.rpartition(separator)
Enter fullscreen mode Exit fullscreen mode

separator*: the part of the string that will trigger the separation. If it occurs more than once in the string, the last occurrence is considered.

Return value

Return a tuple containing three parts:

  • the part of the string before the last occurrence of the separator
  • the separator itself
  • the part of the string after the separator

Example

Python 3.7.4

>>> s = "I feel the need - the need for speed!"
>>> s.rpartition("-")
('I feel the need ', '-', ' the need for speed!')

>>> s.rpartition(",")
('', '', 'I feel the need - the need for speed!')

>>> s = "I feel the need - the need - for speed!"
>>> s.rpartition("-")
('I feel the need - the need ', '-', ' for speed!')
Enter fullscreen mode Exit fullscreen mode

split()

Parameters

string.split(separator, maxsplit)
Enter fullscreen mode Exit fullscreen mode

separator: the element to use in separating the string. If none is specified, the string is separated using spaces.

maxsplit: the maximum number of splits

Return value

Returns a list of strings separated at the specified separator.

Example

Python 3.7.4

>>> s = "I feel the need - the need for speed!"
>>> s.split()
['I', 'feel', 'the', 'need', '-', 'the', 'need', 'for', 'speed!']

>>> s.split("-")
['I feel the need ', ' the need for speed!']

>>> s.split("need")
['I feel the ', ' - the ', ' for speed!']

>>> s.split("need", 1)
['I feel the ', ' - the need for speed!']
Enter fullscreen mode Exit fullscreen mode

rsplit()

Parameters

string.rsplit(separator, maxsplit)
Enter fullscreen mode Exit fullscreen mode

separator: the element to use in separating the string. If none is specified, the string isseparated using spaces.

maxsplit: the maximum number of splits

Return value

Returns a list of strings separated at the specified separator starting from the right.

Example

Python 3.7.4

>>> s = "I feel the need - the need for speed!"
>>> s.rsplit()
['I', 'feel', 'the', 'need', '-', 'the', 'need', 'for', 'speed!']

>>> s.rsplit("-")
['I feel the need ', ' the need for speed!']

>>> s.rsplit("need")
['I feel the ', ' - the ', ' for speed!']

>>> s.rsplit("need", 1)
['I feel the need - the ', ' for speed!']
Enter fullscreen mode Exit fullscreen mode

splitlines()

Parameters

string.splitlines(keepends)
Enter fullscreen mode Exit fullscreen mode

keepends: if included, the line breaks are also included in the returned items of the list.Provided as either True or False. Defaults to False if nothing is provided.

Return value

Returns a list of lines in the string.

Example

Python 3.7.4

>>> s = """“Hope” is the thing with feathers -
... That perches in the soul -
... And sings the tune without the words -
... And never stops - at all -"""

>>> s.splitlines()
['“Hope” is the thing with feathers -', 'That perches in the soul -', 
'And sings the tune without the words -', 'And never stops - at all -']

>>> s.splitlines(True)
['“Hope” is the thing with feathers -\n', 'That perches in the soul -\n', 
'And sings the tune without the words -\n', 'And never stops - at all -']
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .