Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Iterating over a string by 2 characters

Let's say I have the following ascii hex string "4a65737573". How do I now iterate over it, wrapping a fixed amount of characters, but always incrementing by 2. i.e. 4a65, 6573, 7375...

like image 680
Dark Avatar asked Jul 05 '26 00:07

Dark


1 Answers

This should do it:

In [218]: mystr =  "4a65737573"

In [219]: for i in range(0, len(mystr), 2):
   .....:     print mystr[i:i+4]
   .....:     
4a65
6573
7375
7573
73
like image 160
inspectorG4dget Avatar answered Jul 07 '26 14:07

inspectorG4dget



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!