-3

I want to convert python codes to java. But I do not understand slicing step parameter.

Example:

x = "Hello World !"
x[6:2:-1]

And what is the result x[6:2:-1] ?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
N3CAT1
  • 367
  • 4
  • 13

1 Answers1

10

-1 step just reverts the direction of the slice:

>>> x = "Hello World !"
>>> x[6]
'W'
>>> x[2]
'l'
>>> x[6:2:-1]
'W ol'

[6:2:-1] means give me a slice from the 6th to the 2nd item (not including), reversed.

FYI, you don't need python installed for checking the result of the code you've asked about, go to pythonanywhere and play:

PythonAnywhere is a Python development and hosting environment that displays in your web browser and runs on our servers.

Also see:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195