Formatting: From Lists to Strings
Often we write a program to report a single data item, such as a particular element in a corpus that meets some complicated criterion, or a single summary statistic such as a word-count or the performance of a tagger. More often, we write a program to produce a structured result; for example, a tabulation of numbers or linguistic forms, or a reformatting of the original data. When the results to be presented are linguistic, textual output is usually the most natural choice. However, when the results are numerical, it may be preferable to produce graphical output. In this section, you will learn about a variety of ways to present program output.
From Lists to Strings
The simplest kind of structured object we use for text
processing is lists of words. When we want to output these to a
display or a file, we must convert these lists into strings. To do
this in Python we use the join()
method, and specify the string to be used as the “glue”:
>>> silly = ['We', 'called', 'him', 'Tortoise', 'because', 'he', 'taught', 'us', '.'] >>> ' '.join(silly) 'We called him Tortoise because he taught us .' >>> ';'.join(silly) 'We;called;him;Tortoise;because;he;taught;us;.' >>> ''.join(silly) 'WecalledhimTortoisebecausehetaughtus.'
So ' '.join(silly) means:
take all the items in silly and
concatenate them as one big string, using '
' as a spacer between the items. I.e., join() is a method of the string that you want to use as the glue. (Many people find this notation for ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access