-1
n = int(input())  # number of strings
for _ in range(n):
    string = input()
    print(string[::2], string[1::2])  # print even letters, odd letters

Why inside of string[1::2] has a 4 property. So must be 3 property start, end, increment step. And the code running with nothing wrong. Can explain why the string had 4 property not 3, it same thing or different?

unknownit
  • 5
  • 4

1 Answers1

0

The basic syntax for the slice operator is [start:end:step], where end index is excluded. If any of those parameters are missing, default value is used.

For example,

some_string[:len(some_string)] # from 0 to the end of string(step=1 by default)
some_string[:len(some_string):1] # identical to the previous example
some_string[0:len(some_string):1] # also identical to the previous example


some_string[0:] # identical as well(end is len(some_string) by default here)
some_string[0::1] # identical to previous example

As you can see, default values will be used if not provided, which is very useful. I bet all of us would rather write [:] than [0:len(string):1]

P.S This rule changes slightly if your len(string)-1 and other operators are missing. For example,

some_string[len(string)-1:0] # the default value for step will be -1
Alex.Kh
  • 572
  • 7
  • 15