Look at the sequence and determine the output >>> str="Python" >>> str[:-1] A. 'nohtyP' B. 'nhy' C. 'nhyP' D. 'Pytho'
Answers
Answered by
10
String Slices
String Slicing in Python refers to the act of extracting a substring from a given string.
GIVEN PROGRAM:
>>> str = "Python"
>>> str[:-1]
OUTPUT:
It selects all characters of the sequence but the last. It gets all the characters from the string but the last character. represents going through the string implies the last character of the string.
The syntax of string slicing is given below:
Let's take an example to understand better.
str[1:5]
- str[1:5] is 'YTHO' - characters starting at index 1 and extending up to but not including index 5.
str[1:4]
- str[1:5] is 'YTH' - characters starting at index 1 and extending up to but not including index 4.
str[1:3]
- str[1:5] is 'YT' - characters starting at index 1 and extending up to but not including index 3.
str[1:2]
- str[1:5] is 'Y' - characters starting at index 1 and extending up to but not including index 2.
str[:-1]
- str[:-1] is 'PYTHO' - omitting either index defaults to the start or end of the string.
str[:]
- str[:] is 'PYTHON' - omitting both always give us the copy of whole string.
Similar questions