1
substring = lambda s,start=0,end=len(s) : s[start:end]
print(substring("python",1))

I get an error saying s is not defined

NameError                                 Traceback (most recent call last)
<ipython-input-18-7e4b2c9c6d67> in <module>()
----> 1 substring = lambda s,start=0,end=len(s) : s[start:end]
      2 print(substring("python",1,3))

 NameError: name 's' is not defined 

I want to give the start and end as optional variables. How can I do it?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
javaShilp
  • 59
  • 1
  • 6
  • 2
    You can't do that in the definition; you can't in a regular function, either. You'd have to do something like `s[start or 0:end or len(s)]`. But as a rule of thumb: if you're naming the `lambda`, just write a function. – jonrsharpe Jul 17 '18 at 11:20
  • Thanks! Okay.. I will try that way. (I am beginner, will keep in mind the rule) – javaShilp Jul 17 '18 at 11:23

0 Answers0