Jan-05-2021, 05:06 PM
(This post was last modified: Jan-05-2021, 05:14 PM by deanhystad.)
Getting back to the original post question, index() will not work because you can have repeating letters. You could use enumerate to iterate through the user text.
A safer solution is to set a flag to let you know that you are in a break and looking for the start of the next word.
def initialise(userText): firstLetters = userText[0] for index, letter in enumerate(userText): if letter == " ": firstLetters += userText[index+1] return firstLettersA problem with this approach is it assumes the next letter after a space is the start of a word. This fails for us older folks who follow a period with two spaces, and it would crash if you entered a trailing space.
A safer solution is to set a flag to let you know that you are in a break and looking for the start of the next word.
def initialise(userText): punctuation = ' .,:;()"' firstLetters = '' start = True for letter in userText: if letter in punctuation: start = True elif start: firstLetters += letter start = False return firstLetters print(initialise('This is a test. It works for (some) punctuaton.'))