Computer Science, asked by gksaresssuresh, 2 months ago

3. Given st="WELCOME" What will be the value of
a) st[-4]
b) st[0]
c) st[5]
d) st[-1]
in python

Answers

Answered by Equestriadash
54

We have a variable st with the value 'WELCOME'.

a) st[-4] will give the output:

  • 'C'

b) st[0] will give the output:

  • 'W'

c) st[5] will give the output:

  • 'M'

d) st[-1] will give the output:

  • 'E'

String indexing/string slicing is using index values to obtain a substring from a given string.

The index values represent the characters in the string.

  • Positive indexing [left to right] starts from 0.
  • Negative indexing [right to left] starts from -1.

For the string 'WELCOME', the positive indexing will look like this:

\begin{array}{|c|c|}\cline{1-2} \tt Character & \tt Index\ Value\\\cline{1-2} \sf W & 0\\\cline{1-2} \sf E & 1\\\cline{1-2} \sf L & 2\\\cline{1-2} \sf C & 3\\\cline{1-2} \sf O & 4\\\cline{1-2} \sf M & 5\\\cline{1-2} \sf E & 6\\\cline{1-2}\end{array}

The negative indexing will look like this:

\begin{array}{|c|c|}\cline{1-2} \tt Character & \tt Index\ Value\\\cline{1-2} \sf W & -7\\\cline{1-2} \sf E & -6\\\cline{1-2} \sf L & -5\\\cline{1-2} \sf C & -4\\\cline{1-2} \sf O & -3\\\cline{1-2} \sf M & -2\\\cline{1-2} \sf E & -1\\\cline{1-2}\end{array}

Answered by BrainlyCyclone
27

Answer:

Answer :-

a) st[-4]

'C'

b) st[0]

'W'

c) st[5]

'M'

d) st[-1]

'E'

Positive Value :-

 \begin{gathered}\begin{array}{|c|c|}\cline{1-2} \sf Character & \sf Index\ Value\\\cline{1-2} \sf W & 0\\\cline{1-2} \sf E & 1\\\cline{1-2} \sf L & 2\\\cline{1-2} \sf C & 3\\\cline{1-2} \sf O & 4\\\cline{1-2} \sf M & 5\\\cline{1-2} \sf E & 6\\\cline{1-2}\end{array}\end{gathered}

Negative Value

\begin{gathered} \begin{array}{|c|c|}\cline{1-2} \sf Character & \sf Index\ Value\\\cline{1-2} \sf W & -7\\\cline{1-2} \sf E & -6\\\cline{1-2} \sf L & -5\\\cline{1-2} \sf C & -4\\\cline{1-2} \sf O & -3\\\cline{1-2} \sf M & -2\\\cline{1-2} \sf E & -1\\\cline{1-2}\end{array}\end{gathered}

Know Møre :-

Python is a high Language programming. It is used to program mark,words and some other things on it. In Python can be array created.

Range of python is (). It has 0 as default

 \\  \\

Similar questions