Computer Science, asked by balanighi21, 4 months ago

Select the correct output of the following String operations : str1 = 'Welcome' print (str1[:6] + ' PYnative') *

2 points

Welcome PYnative

WelcomPYnative

Welcom PYnative

WelcomePYnative

Answers

Answered by Equestriadash
11

Given statements:

  • str1 = 'Welcome'
  • print(str1[:6] + ' PYnative')

Output:

  • Welcom PYnative

Explanation:

The above act is done using slicing, which in this case is, extracting a substring from a given string.

In order to do so, one must know the index values and how it is ordered.

A slice statement follows a similar syntax:

  • string_name[start:stop:step]

Where,

  • start - indicates the starting index position
  • stop - indicates the ending index position
  • step - indicates by how much the values must skip

If no start value is given, by default, it will start from the first character, that is under index position 0.

Here, the index values for the string "Welcome", would be as follows.

\begin{array}{|c|c|}\cline{1-2}\bf String & \bf 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}

Since the slice given was str1[:6], it will start from 0 and end at 5.

NOTE: The last index value is NOT counted.

Which means, it will start from 'W' and end at 'm'.

The next part is a simple concatenation. Which is just adding two strings together.

The output will hence be:

Welcom PYnative

Similar questions