[Python] Why is slicing a string not working if step is not mentioned?

Discussão em 'Python' iniciado por Stack, Maio 1, 2025.

  1. Stack

    Stack Membro Participativo

    I am studying slicing in strings using the slicing operator :.

    The string I have chosen is word = 'fabricated'.

    According to me, the output for the below statements:

    print(word[5:-8])
    print(word[-3:-9])


    should be:

    cir
    tacirb


    However, nothing is getting printed on running these statements. Neither on Google Colab nor on VS Code.

    But when I specifically mention the step parameter as follows:

    print(word[5:-8:-1])
    print(word[-3:-9:-1])


    I get my expected output as:

    cir
    tacirb


    I would like to understand why I am getting a string of length 0 although the step parameter is not mandatory.

    Continue reading...

Compartilhe esta Página