I got the following string while studying Chapter 6: Manipulating Strings in 'Automate the Boring Stuff with Python' by AI Sweigart:
text = '''Lists of animals
Lists of aquarium life
Lists of biologists by author abbreviation
Lists of cultivars'''
I am expected to convert the string to a bulleted list.
I have split the string using new line as the separator, according to the author's guidelines, then append the list into an empty list.
lines = text.split('\n')
textList = []
for x in lines:
textList.append(x)
How can I add bullets to the beginning of each item? The author wants the list to appear as follows:
* Lists of animals
* Lists of aquarium life
* Lists of biologists by author abbreviation
* Lists of cultivars
You could do:
text = "\n".join(["* " + substr for substr in text.split("\n")])
This adds an asterisk to every line in text
by splitting text
by newlines, adding an asterisk and then joining them with a newline in between.
Output:
* Lists of animals
* Lists of aquarium life
* Lists of biologists by author abbreviation
* Lists of cultivars
However, if you want the text as a list of lines instead of a text block, you just omit the "\n".join()
:
text = ["* " + substr for substr in text.split("\n")]