0

I have a string variable L and I need to print the 18th symbol, then the 36th, and so on in 18-character steps. They must be printed in the opposite case from the original letter (print A if the original letter is a, print a if A, etc.), along with the position of that letter in the string:

o   18,

o   36E

o   54 

o   72I

I know how to get the 18th symbol in the string, but how do I process each 18th symbol? Can I do this using L[::18]? And I don't really know how to transform from a to A.

KernelPanic
  • 600
  • 8
  • 19

1 Answers1

1

Yes, [::18] will get the characters you want. After that, use the isupper and islower methods to find out what you have; use upper and lower methods to change.

Here's some simple test code to see how things work. Adjust as needed.

test = "0123456789ABCDEFGH"*4

print "with a string", test[::18]

print "... and with a loop:"
for i in range(0,len(test), 18):
    print i, test[i]                             

Output:

with a string 0000
... and with a loop:
0 0
18 0
36 0
54 0
Prune
  • 76,765
  • 14
  • 60
  • 81
  • and what about adding the position of the letter in the string? – Igor Ostapuk Mar 01 '17 at 00:24
  • That's a simple linear translation; can you do the math? Alternately, you could set up a **for** loop with the desired values and thereby have the index (position) already generated. – Prune Mar 01 '17 at 00:27