0

I have the following:

if line.strip().startswith("#") or line.strip().startswith('"""'):

I don't really want to declare a variable before to hold line.strip() nor repeat it twice in the if is there a way to hold it inside of the if something like:

if <something> _.startswith("#") or _.startswith('"""'):
MiguelP
  • 416
  • 2
  • 12

1 Answers1

2

Yes! The Walrus operator!

if (x := line.strip()).startswith("#") or x.startswith('"""'):
juanpethes
  • 831
  • 2
  • 18