At times we want to convert a text into individual elements and to convert a list of words into texts
Let's say we want to convert the following text into a list of words. Let's have the famous Lorem ipsum text. This text is a scrambled version of De finibus bonorum et malorum, a 1st century BC Latin text by Cicero.
>>> text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
#By default the split is done using space as delimiter
>>> words_space = string.split(text)
>>> words_space
['Lorem', 'ipsum', 'dolor', 'sit', 'amet,', 'consectetur', 'adipiscing', 'elit,', 'sed', 'do', 'eiusmod', 'tempor', 'incididunt', 'ut', 'labore', 'et', 'dolore', 'magna', 'aliqua.']
#Passing , as delimiter
>>> words_comma = string.split(text,',')
>>> words_comma
['Lorem ipsum dolor sit amet', ' consectetur adipiscing elit', ' sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.']
Now let's join them back
>>> text_space = string.join(words_space)
>>> text_space
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
#inserting a delimiter in joined string
>>> text_colon
'Lorem ipsum dolor sit amet: consectetur adipiscing elit: sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
Let's say we want to convert the following text into a list of words. Let's have the famous Lorem ipsum text. This text is a scrambled version of De finibus bonorum et malorum, a 1st century BC Latin text by Cicero.
>>> text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
#By default the split is done using space as delimiter
>>> words_space = string.split(text)
>>> words_space
['Lorem', 'ipsum', 'dolor', 'sit', 'amet,', 'consectetur', 'adipiscing', 'elit,', 'sed', 'do', 'eiusmod', 'tempor', 'incididunt', 'ut', 'labore', 'et', 'dolore', 'magna', 'aliqua.']
#Passing , as delimiter
>>> words_comma = string.split(text,',')
>>> words_comma
['Lorem ipsum dolor sit amet', ' consectetur adipiscing elit', ' sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.']
Now let's join them back
>>> text_space = string.join(words_space)
>>> text_space
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
#inserting a delimiter in joined string
>>> text_colon
'Lorem ipsum dolor sit amet: consectetur adipiscing elit: sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
No comments:
Post a Comment