[Python] Issue Appending " to a List in Python

Discussão em 'Python' iniciado por Stack, Maio 26, 2025.

  1. Stack

    Stack Membro Participativo

    I am attempting to split a string by each word and symbol into a list. This should put them in separate strings by iteration in the function SplitString().

    For example:

    display("Hello, World!") -> ['display', '(', '"', 'Hello', ',', 'World', '!', '"', ')']

    However, in my code, the double quotation symbol (") does not get appended to the list. It shows as this instead:

    ['display', '(', 'Hello', ',', 'World', '!', '', ')']

    Here is my code:

    string = 'display("Hello, World!")'


    def SplitString(string):
    splitstring = []
    newword = ''
    firstIteration = True
    symbolPassed = False # Initialize here

    for i in string:
    if newword == '' and firstIteration == False and symbolPassed == True:
    symbolPassed = False

    elif i == ' ':
    if newword:
    splitstring.append(newword)
    newword = ''
    symbolPassed = False

    elif i in '!?+-*/=<>()}{][.,\":;@£$%^&|¬`~_€]':
    splitstring.append(newword)
    newword = ''
    splitstring.append(i)
    newword = ''
    symbolPassed = True

    else:
    newword += i
    symbolPassed = False

    firstIteration = False


    if newword:
    splitstring.append(newword)

    return splitstring

    SplitedString = SplitString(string)


    for i in range(0, len(SplitedString)):
    print(SplitedString)
    print(SplitedString)

    Continue reading...

Compartilhe esta Página