pythonlistlinear-search

How do I read a set of characters from a specific position in a string in python


So I am writing my own cypher and decoder for a bit of fun but I've gotten stuck on my decipher program where I need to move up by 1 character in my string in order to decipher the next "block" of characters.

cyinput2 = ",#96c8a2: ,#808000: ,#96c8a2: ,#e1a95f: ,#808000: ,#6f00ff:"
#989E86

Above is my string that I am trying move up in, essentially I am trying to move on from my first base 6 string to the second, which in this case would be ",#808000" and then so on by increments of 10 characters from each comma.

I've tried to incorporate some ideas such as just adding 1 character to count as the space in between each base 6 string but I wasn't able to figure out. I have also started to work on using a variable set to integer 0 that would increment by 10 after each string is converted back into hexadecimal. But I haven't yielded any results from that yet, so I'm hoping someone on here may be able to stop me from going overboard when I could instead use a much simpler solution.

while str(cyinput2) != "":

    S = 0
    S1 = 9

    hexstart = ','
    hexend = ':'

    start_index = cyinput2.find(hexstart) + len(hexstart)
    end_index = cyinput2.find(hexend)

    block = cyinput2[start_index:end_index]
    print(block) 

Solution

  • You could simply convert your string to a list by using split, If you want clean blocks you can use replace too

    # Removing useless chars in order to have a clean split
    cyinput2 = cyinput2.replace(',', '')
    cyinput2 = cyinput2.replace(':', '')
    # From : ",#96c8a2: ,#808000: ,#96c8a2: ,#e1a95f: ,#808000: ,#6f00ff:"
    # to :   "#96c8a2 #808000 #96c8a2 #e1a95f #808000 #6f00ff"
    
    # Split
    cyinput2 = cyinput2.split(' ')
    # From : "#96c8a2 #808000 #96c8a2 #e1a95f #808000 #6f00ff"
    # to : ["#96c8a2", "#808000", "#96c8a2", "#e1a95f", "#808000", "#6f00ff"]
    for block in cyinput2:
        print(block)