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()
:
>>> mylist = ['spam', 'egg', 'sausage', 'bacon', 'lobster']
>>> mylist
['spam', 'egg', 'sausage', 'bacon', 'lobster']
>>> '; '.join(mylist)
'spam; egg; sausage; bacon; lobster'
>>> ','.join(mylist)
'spam,egg,sausage,bacon,lobster'
>>> word = 'lobster'
>>> type(word)
<class 'str'>
>>> ':'.join(word)
'l:o:b:s:t:e:r'
>>> mylist2 = ['spam', 23, 'egg']
>>> type(mylist2)
<class 'list'>
>>> ', '.join(mylist2)
Traceback (most recent call last):
File "<input>", line 1, in <module>
', '.join(mylist2)
TypeError: sequence item 1: expected str instance, int found
>>> mylist3 = ['spam', str(23), 'egg']
>>> ', '.join(mylist3)
'spam, 23, egg'
Here’s how to use str.partition()
:
>>> s = 'egg.spam'
>>> s.partition('.')
('egg', '.', 'spam')
>>> t = 'egg$$spam$$bacon'
>>> t.partition('$$')
('egg', '$$', 'spam$$bacon')
Here’s how to use str.rpartition()
:
>>> t = 'egg$$spam$$bacon'
>>> t.rpartition('$$')
('egg$$spam', '$$', 'bacon')
>>> t.partition('.')
('egg$$spam$$bacon', '', '')
>>> t.rpartition('.')
('', '', 'egg$$spam$$bacon')
Here’s how to use str.split()
:
>>> s = 'spam bacon sausage egg'
>>> s.split()
['spam', 'bacon', 'sausage', 'egg']
>>> s = 'spam\tbacon\nsausage egg'
>>> s.split()
['spam', 'bacon', 'sausage', 'egg']
>>> t = 'spam.bacon.sausage.egg'
>>> t.split('.')
['spam', 'bacon', 'sausage', 'egg']
>>> t = 'bacon...lobster...bacon'
>>> r.split('.')
['bacon', '', '', 'lobster', '', '', 'bacon']
>>> q = 'bacon\n lobster\t\n egg'
>>> q.split()
['bacon', 'lobster', 'egg']
>>> link = 'www.realpython.com'
>>> link.split('.', maxsplit=1)
['www', 'realpython.com']
Here’s how to use str.rsplit()
:
>>> link = 'www.realpython.com'
>>> link.rsplit('.', maxsplit=1)
['www.realpython', 'com']
>>> link.split('.')
['www', 'realpython', 'com']
>>> link.rsplit('.')
['www', 'realpython', 'com']
Here’s how to use str.splitlines()
:
>>> moby = 'Call me Ishmael.\nSome years ago- never mind how long precisely-\nhaving little or no money in my purse,\nand nothing particular to interest me on shore,\nI thought I would sail about a little and see the watery part of the world.\n'
>>> mobysplit = moby.splitlines()
>>> mobysplit
['Call me Ishmael.', 'Some years ago- never mind how long precisely-', 'having little or no money in my purse,', 'and nothing particular to interest me on shore,', 'I thought I would sail about a little and see the watery part of the world.']
>>> mobysplit[0]
'Call me Ishmael.'
>>> mobysplit[1]
'Some years ago- never mind how long precisely-'
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