Computer Science, asked by hetalkothari93, 5 days ago

354
Consider the string str = "Save water and save earth"
Write statements in python to implement the following:
(a) To display one character of the word
(b) To get the first three characters
(c) To get the last three characters
(d) To get all but the three first character
(e) To get all but the three last characters

Answers

Answered by XxANKUxX
0

(a) To display one character of the word

(b) To get the first three characters

(c) To get the last three characters

(d) To get all but the three first character

(e) To get all but the three last characters

Answered by SaurabhJacob
0

The statements in python language are,

For (a),

                     print(str[0])

                     for i in range(1,len(str)):

                       if(str[i]==' '):

                        print(str[i+1])

For (b),

                     print(str[:3])

For (c),

                     print(str[len(str)-3:])

For (d),

                     print(str[3:])

For (e),

                     print(str[:len(str)-3])

  • Loop is used in the first statement for traversing the string and finding out the spaces and then printing the letter after that.
  • String Slicing is used after the first statement.
  • Its syntax is as,

                                  string_name [start : stop ]

  • If a start is not provided then it starts from the beginning.
  • If an end is not given then it ends at the end of the string.
Similar questions