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)
str.isalpha, maybe it could be useful"i" after "!?"+-*/=<>()}{][.,\":;@£$%^&|¬~_€]':` This might be the topnewword = ''could be inside `elif i =='``.