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.
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
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.'
' '.join(silly) means:
take all the items in
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 ...