0

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[i])
print(SplitedString)
4
  • 1
    the problem is the logical structure of the program not of the quotation mark. Check for str.isalpha, maybe it could be useful Commented May 26 at 16:16
  • 1
    You would probably find it useful to start to use a debugger. Try Python Tutor for small codes or else one of the more sophisticated tools readily available. Commented May 26 at 16:21
  • Why did you include "i" after "!?"+-*/=<>()}{][.,\":;@£$%^&|¬~_€]':` This might be the top Commented May 26 at 16:25
  • The loop may have newword = '' could be inside `elif i =='``. Commented May 26 at 16:33

2 Answers 2

0

There is something strange about your loop and how it is handling special characters. I'll leave it to you to debug, but I am pasting an example of the code below where I have added some helpful debugging print statements. I have also added an extra quote mark (") to your original string. When this happens it skips a quote mark and the letter H from the original string. This makes me think that something about how you are iterating over characters is not handling the specials well. I also moved the / to a different location because /" creates an escaped quote mark and that may not be the behavior you were looking for. Finally, there is probably more efficient ways to do this, but I like the idea of sticking with your plan and getting a working output :)

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


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

    for i in string:
        print(f"Cur char: {i}")

        if newword == '' and firstIteration == False and symbolPassed == True:
            symbolPassed = False


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

        elif i in '!?+-*/\=<>()}{][.,":;@£$%^&|¬`~_€]':
            print(f"Found Special Character {i}")
            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[i])
print(SplitedString)

Notably, take a look at this output in the console for how your program behaves:

Cur char: (
Found Special Character (
Cur char: "
Cur char: "
Found Special Character "
Cur char: H

And the final output it produces when this happens:
['display', '(', '', '"', 'ello', ',', 'World', '!', '', ')']

Sign up to request clarification or add additional context in comments.

Comments

0

Use regex:

import re
re.split('(?=\\W)|(?<=\\W)(?=\\w)', string)

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

Note:

This uses a positive lookahead for a non-word (?=\W) and a positive lookbehind for a non-word followed by a word (?<=\W)(?=\w), then splits the string on these positions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.