Python | Word Stretch

Python | Word Stretch

If you want to create a "word stretch" effect in Python, where each letter of a word is repeated a number of times based on its position in the word, you can use a simple loop to iterate over the characters of the word and repeat each character. Here's a function that does just that:

def stretch_word(word): stretched = '' for i, char in enumerate(word): stretched += char * (i + 1) return stretched # Example usage print(stretch_word("hello")) # Output: "heellllllooooo" 

In this function, enumerate(word) is used to iterate over each character in the word along with its index i. The character is repeated i + 1 times, creating the stretch effect. For instance, the first character is not repeated (i + 1 = 1), the second character is repeated once (i + 1 = 2), and so on.


More Tags

topshelf office365 proxy-authentication matrix-multiplication oledb hcatalog avcapturesession qt hessian android-design-library

More Programming Guides

Other Guides

More Programming Examples