[Python] Converting string to Bulleted List in Python

Discussão em 'Python' iniciado por Stack, Fevereiro 19, 2025.

  1. Stack

    Stack Membro Participativo

    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. Thereafter, I added bullets to the beginning of each item using the following code:

    lines = text.split('\n')
    textList = []
    for x in lines:
    textList.append(x)
    for y in textList:
    newList = '* ' + y
    print(newList)


    Then the result I got is:

    • Lists of cultivars

    Which is the last item in the list. Whereas, 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

    Please I need help on how to achieve this in Python.

    Continue reading...

Compartilhe esta Página