How do I get a substring of a string in Python?

前端 未结 13 1910
我寻月下人不归
我寻月下人不归 2020-11-22 00:32

Is there a way to substring a string in Python, to get a new string from the third character to the end of the string?

Maybe like myString[2:end]?

13条回答
  •  不思量自难忘°
    2020-11-22 01:19

    a="Helloo"
    print(a[:-1])
    

    In the above code, [:-1] declares to print from the starting till the maximum limit-1.

    OUTPUT :

    >>> Hello
    

    Note: Here a [:-1] is also the same as a [0:-1] and a [0:len(a)-1]

    a="I Am Siva"
    print(a[2:])
    

    OUTPUT:

    >>> Am Siva
    

    In the above code a [2:] declares to print a from index 2 till the last element.

    Remember that if you set the maximum limit to print a string, as (x) then it will print the string till (x-1) and also remember that the index of a list or string will always start from 0.

提交回复
热议问题