Splitting A String Into Words And Punctuation Without Using Any Import
I've seen similar questions to my question, but they all used regex.What I want to do is taking input such as 'Wow,this was really helpfull!' and return to 'Wow , this was really h
Solution 1:
Your question is not 100% clear as in if you want a second space after a punctuation if two appear right after another.
But assuming you are fine with two spaces, the code could look something like this:
sentence_in = "Wow,this was really helpful!"
sentence_out = ""
punctuation = "!\"#$%&'()*+,-./:;<=>?@[\]^`{|}~"
for character in sentence_in:
if character in punctuation:
sentence_out += " %s " % character
else:
sentence_out += character
print(sentence_out)
The problem with your code is that it is not correctly indented which is important in Python since it is used to indicate a code block. For example see:
for punc in sentence :
if punc in punctuation :
outputpunc = " %s" % punc
else :
outputpunc = character
Should really look like this:
for punc in sentence :
if punc in punctuation :
outputpunc = " %s" % punc
else :
outputpunc = character
As you can see the rest of the code after the beginning of the for-loop needs to be indented. After you are finished with your loop you can go back to the same level of indentation as before.
Post a Comment for "Splitting A String Into Words And Punctuation Without Using Any Import"