- Get link
- X
- Other Apps
Slicing The slice syntax can be utilized to obtain a selection of characters within a range. To obtain a specific portion of a string, indicate the beginning index and the ending index separated by a colon using the slice notation. Example Retrieve the characters starting from index 3 up to but not including index 7: x = "Python slicing" print (x[ 3 : 7 ]) Note: The first character has index 0. Slice From the Start If you don't specify the starting index, the range will start from the first character: Example Retrieve the first 5 characters of the string, excluding the character at the 6th position: x = "Python slicing" print (x[: 6 ]) Slice To the End If you exclude the end index when defining a range, it will automatically include all the elements until the end : Example Retrieve all the characters from the 3rd position to the end of the string: x = "Python slicing" print (x[ 3 :]) Negative Indexing To begin the slice from the end of the strin...