1

I am new to python. Please anybody explain the following string operations

s="abcdefghijklmnop"

print s[:6][::-1] #is it first calculating s[:6] and then operating the result with [::-1] ?

Jan Vorcak
  • 19,261
  • 14
  • 54
  • 90
user2572943
  • 135
  • 1
  • 2
  • 10

1 Answers1

15
"abcdefghijklmnop"[:6][::-1]
  1. Take first 6 characters (abcdef).

  2. Read the result from the end to the beginning (fedcba).

There is an other better way to get this result:

"abcdefghijklmnop"[5::-1]
Happy
  • 1,815
  • 2
  • 18
  • 33