'Group letters of a sentence
I have a question about grouping letters in a string. For example, I want to group a sentence: my name is Jhon and I want something like this: myname ynamei nameis, and so on...
My code is:
def letters(siq,data):
n=len(siq)
for i in range(data -1, n -6, 1):
yield siq[i:i+6]
test = 'My name is Jhon'
for i in range(1,4):
print(list(letters(test,i)))
Solution 1:[1]
You can preprocess the string by removing each space using .replace(), and then lowercasing the string using .lower().
Then, to extract six-character substrings, you can use zip() along with list unpacking. This approach makes it easier to dynamically set the length of the substrings that you want to extract.
data = 'My name is Jhon'
data = data.replace(' ', '').lower()
substring_length = 6
print(list(''.join(excerpt) for excerpt in zip(*[data[i:] for i in range(substring_length)])))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | BrokenBenchmark |
