Computer Science, asked by kaypeeyes2004, 1 month ago

For the given string str = "Hello PYTHON" write the positive & negative index and also write the OP for the following statement

str 71 =

str [1] str [- 3]​

Answers

Answered by Equestriadash
5

str = 'Hello PYTHON'

Positive indexing:

\begin{array}{|c|c|}\cline{1-2}\tt Character & \tt Index\ Value\\\cline{1-2}\sf H & -0\\\cline{1-2} \sf e & 1\\\cline{1-2}\sf l & 2\\\cline{1-2}\sf l & 3\\\cline{1-2}\sf o & 4\\\cline{1-2} & 5\\\cline{1-2}\sf P & 6\\\cline{1-2}\sf Y & 7\\\cline{1-2}\sf T & 8\\\cline{1-2}\sf H & 9\\\cline{1-2}\sf O & 10\\\cline{1-2}\sf N & 11\\\cline{1-2}\end{array}

Negative indexing:

\begin{array}{|c|c|}\cline{1-2}\tt Character & \tt Index\ Value\\\cline{1-2}\sf H & -12\\\cline{1-2} \sf e & -11\\\cline{1-2}\sf l & -10\\\cline{1-2}\sf l & -9\\\cline{1-2}\sf o & -8\\\cline{1-2} & -7\\\cline{1-2}\sf P & -6\\\cline{1-2}\sf Y & -5\\\cline{1-2}\sf T & -4\\\cline{1-2}\sf H & -3\\\cline{1-2}\sf O & -2\\\cline{1-2}\sf N & -1\\\cline{1-2}\end{array}

Output for str[1]:

  • 'e'

Output for str[-3]:

  • 'H'

String slicing refers to the act of retrieving a substring from a given string. It is done using index values. Positive indexing indicates that the string is being traversed from left to right, while negative indexing traverses from right to left.

Similar questions