0

sorry if i worded the question wrong, but I am trying to figure out how to split user input into into separate strings each containing only 2 letters so an input of "hello world" would look something like ["he", "el", "lo", " w", "or", "ld"]

i am using this for a program i am creating and this block of code would is crucial to its functionality. If anyone has any questions, advice, or anything else i am willing respond, Thanks.

PS. I already created the code that ads a space at the end of the user input in the event that there are an odd number of characters so please let me know if there is a better way of doing this.

AJF
  • 19
  • 2
  • 2
    Shouldn't the expected output be `['he', 'll', 'o ', 'wo', 'rl', 'd']` instead? Or do you actually mean to repeatedly pair each character with its preceding character? – blhsing Oct 26 '18 at 02:58

1 Answers1

2
s = '123'
[s[i:i+2] for i in range(0,len(s),2)]

it works for both even length and odd length

Boyang Li
  • 157
  • 2
  • 11