So how did I go about solving this problem?
I first started by pseudocoding my steps on what I needed to do:
- # Trim first two letters of word "stut"
- # loop 2x + elipses
- # combine the stut with the word, and a question mark.
- # stutter("outstanding") ➞ "ou... ou... outstanding?"
I knew of a few directions I could go about solving this, but I wasn't positive on what these methods were in Python 3, so I went to Google to search for some answers.
My first hunch was to use .strip()
, so I searched "Python strip string". No.. this is helpful for removing whitespace (something I believe is worth it if we are truly accepting user input, but not a requirement for this challenge).
So next I thought of .substring()
which made sense, because I just need a subset of the string. The syntax for this is pretty easy to remember in Python, simply use square brackets with where you want to start and stop the substring. word[0:2]
Lastly, I needed to format this. I could assume that this method will always return the same format of string, so I decided to keep that in place, and just grab what's from the argument to return with the string. I had originally thought of using a loop (as mentioned above in my psuedocode), but I later reconsidered.
Solution:
def stutter(word):
stut = word[0:2]
return_string = "{0}... {0}... {1}?".format(stut, word)
return return_string
This could be refactored to not really needing the return_string
def stutter(word):
stut = word[0:2]
return "{0}... {0}... {1}?".format(stut, word)
Like… do we even need any variables to hold the stut
? Nope!
def stutter(word):
return "{0}... {0}... {1}?".format(word[0:2], word)