English

The code provided below is intended to remove the first and last characters of a given string and return the resulting string. However, there are syntax and logical errors in the code. - Computer Science (Python)

Advertisements
Advertisements

Question

The code provided below is intended to remove the first and last characters of a given string and return the resulting string. However, there are syntax and logical errors in the code.

Rewrite it after removing all the errors. Also, underline all the corrections made.

define remove_first_last(str):
   if len(str)<2:
      return str
   new_str = str[1:-2]
   return new_str
result = remove_first_last("Hello")
Print("Resulting string:"result)
Correct and Rewrite
Advertisements

Solution

def remove_first_last(str):
  if len(str)<2:
      return str
  new_str = str[1:−1]
  return new_str
result = remove_first_last("Hello")
print("Resulting string:",result)

shaalaa.com
  Is there an error in this question or solution?
2025-2026 (March) Board Sample Paper
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×