Question: In python. Write a function substring(string, ch) thatreturn the first substring from string that starts andend with a single character ch. The function shouldreturn None

In python. Write a function substring(string, ch) thatreturn the first substring from string that starts andend with a single character ch. The function shouldreturn None if it can’t find any substring that startsand ends with ch, or return the first substring if more thanone exists.

Note the following:

  • capital and a small case of the same letter should be treatedas two different characters.
  • the returned substring must at least be of length two

Function header: substring(string, ch)

Input: a string string anda single character ch.

Output: the first (left-most) substringthat starts and ends with thecharacter ch or None if no such substringexists.

Examples:

>>> string = "aaa#bb#zzzz#kkkkkkk#"

>>> ch= "#"

>>> substring(string, ch)

'#bb#'

>>> string = "fff@sss@aaa@ggg@"

>>> ch= "@"

>>> substring(string, ch)

'@sss@'

>>> string = "wwklmonw$%^&www"

>>> ch= "w"

>>> substring(string, ch)

'ww'

>>> string = "qaaa"

>>> ch= "7"

>>> substring(string, ch)

None

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!