Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Converting Between Strings and Lists

Give Feedback

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-'

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

Chris Bailey RP Team on Dec. 31, 2019

Thanks km, I’m glad you liked the lesson! You ask a good question. I looked at the python documentation, to see if there was an answer I could readily find. I didn’t really find a specific reason. The one reason I could think of for .partition() returning a tuple is that it will always return 3 items. Tuples are immutable, and are more efficient meaning faster and use less memory. When designing methods that are parts of the fundamental pieces of the language, in this case the string type, they would want it to be as efficient as possible. For .split() the number of items returned can vary and its possible that the best way to work with that method is to have a list which is mutable.

Become a Member to join the conversation.