For an assignment I have to print lines from a text that start with "W" and "Z" and end in "n" and "t" (so W-n, W-t, Z-n, Z-t combo's). I have a code now that works, but it seems a bit long and I was wondering if there is a way to shorten it?
This is the code:
import sys
def main():
for line in sys.stdin:
line = line.rstrip()
if line.startswith("W") and line.endswith("n"):
print(line)
if line.startswith("W") and line.endswith("t"):
print(line)
if line.startswith("Z") and line.endswith("n"):
print(line)
if line.startswith("Z") and line.endswith("t"):
print(line)
main()
As I said, it works, but it seems a bit elaborate. Any tips on how to shorten?
I tried line.startswith("Z","W")
and line.endswith("t","n")
but I get a Type Error (all slice indices must be integers or None or have an __index__method).