1

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).

Chen A.
  • 10,140
  • 3
  • 42
  • 61

5 Answers5

6

You could do this:

line = line.rstrip()
if line and line[0] in "WZ" and line[-1] in "nt":
    print(line)

Or use regular expressions:

import re 
# ...
if re.match(r'^[WZ].*[nt]$', line):
    print(line)

# ^ beginning of string
# $ end of string
# [WX] matches W or X
# .*  any character 0 or more times

See the docs on Python regex syntax.

user2390182
  • 72,016
  • 6
  • 67
  • 89
3

startswith and endswith also accept a tuple as argument (see doc). So this will work:

line.startswwith(('W', 'Z'))
line.endswith(('t','n'))

Your code could shorten to:

import sys

def main():

    for line in sys.stdin:
        line = line.rstrip()
        if line.startswith(("W", "Z")) and line.endswith(("n", "t")):
                print(line)

main()
Simpom
  • 938
  • 1
  • 6
  • 23
0

Use regex.

import re

pattern = '[ZW].*[nt]$'

for line in sys.stdin:
    line = line.rstrip()
    mo = re.match(p, line)
    if mo:
        print(line)

Pattern explained:

  • [ZW] matches one of the two characters; because we use re.match, it matches only if the string begins with the pattern. So it has to start with Z or W
  • .* matches anything; char, int, space, etc. one or more times
  • [nt]$ means match n or t (lower case) and $ makes it a match only if the line ends with these values
Chen A.
  • 10,140
  • 3
  • 42
  • 61
0

or you could use a regex

import re

r = re.compile(r^"[WZ].+[nt]$")

m = r.match('Woot')
m.group()
David Jenkins
  • 461
  • 3
  • 8
0

str.startswith allows you to supply a tuple of strings , you have to use a tuple ("Z", "W") not "Z", "W".

import sys
def main():
    for line in sys.stdin:
    line = line.rstrip()
    if line.startswith(("Z","W")) and line.endswith(("t","n")):
       print(line)
main()
Solomon
  • 626
  • 1
  • 5
  • 16