Computer Science, asked by neha415288, 5 months ago

Consider the string str="Green Revolution".write statements in python to implement the following:
To display the last four characters.
To display the starting index for the substring 'vo.'
To check whether the string contains 'vol' or not
To repeat the string 3times

Answers

Answered by Equestriadash
9

str = "Green Revolution"

a) To display the last four characters.

>>>  str[-4:]

'tion'

  • This can be done using string slicing, which is the act of extracting a substring from a given string. It can be used when one is aware of the index values of each character.

b) To display the starting index for the substring 'vo'.

>>> str.index("vo")

8

  • The index() method is used to check whether a given element is present in a string/list or not. If it is, it displays the index value of the particular element.

c) To check whether the string contains 'vol' or not.

>>> "vol" in str

True

  • This is done using a membership operator [in]. It is used for checking whether an element is present in a given string/list/dictionary.

d) To repeat the string 3 times.

>>> str*3

'Green RevolutionGreen RevolutionGreen Revolution'

Similar questions