In this lesson, you’ll explore string methods that convert between a string and some composite data type by either pasting objects together to make a string, or by breaking a string up into pieces. These methods operate on or return iterables, the general Python term for a sequential collection of objects.
Many of these methods return either a list or a tuple. A list encloses the collection of objects in square brackets ([]
) and is mutable. A tuple encloses its objects in parentheses (()
) and is immutable.
Here are methods for converting between strings and lists:
str.join(<iterable>)
str.partition(<sep>)
str.rpartition(<sep>)
str.split(sep=None, maxsplit=-1])
str.rsplit(sep=None, maxsplit=-1])
str.splitlines([<keepends>])
Here’s how to use str.join()
:
km on Dec. 31, 2019
Thanks a lot, another great video. One query: Why is that some methods return TUPLES and some LIST. EG: Partition returns tuple but split returns LIST