Tag Archives: for loop in strings

How to solve a string “splosion” problem in Python using for loop

First of all, what is a string “splosion”. Let’s consider a non-empty string, “hello” for example. You will need to return a string like “hhehelhellhello”.

def stringSplosion(initial_string):
	resulted_string = ""
	for s in range(len(initial_string)):
		resulted_string = resulted_string + initial_string[:s+1]
	return resulted_string

So, we declare a function called stringSplosion which takes in an argument, the string initial_string.
Within the function we create an empty string which is going to be our result.
Now, we start iterating over the initial string. Every time we iterate we add the characters from the beginning of the string up to the s plus 1.

Continue reading