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
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.
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
History,
3 months ago
English,
3 months ago
Science,
3 months ago
Biology,
11 months ago
Social Sciences,
11 months ago