-1
names = ["Adam", "Maria", "Kevin", "Madison", "John", "Bailey"]

I want all the items in this string to print except the names that begin with the letter M. How do I do that. I just started an online course and I am trying to figure this part out. I have re-watched some of the videos and seem to miss the information that will get everything to print that does not start with M.

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
  • 1
    Try to create a string and see how you can index to print only certain letter of the string. Once you have worked that out then you can just use a loop. – sai Oct 30 '20 at 22:22
  • `[x for x in names if not x.startswith("M")]` – Mateen Ulhaq Oct 30 '20 at 22:23
  • 3
    We don't do you homework. Given a string, how do you get its first character? How do you check if that first character is a M? – Frank Yellin Oct 30 '20 at 22:23

2 Answers2

1

Try this:

for name in names:
    if not name.startswith('M'):
        print(name)

If you want to check for lowercase m as well, you can do if not name.lower().startswith('m'): instead.

Tom Karzes
  • 22,815
  • 2
  • 22
  • 41
0
names = ["Adam", "Maria", "Kevin", "Madison", "John", "Bailey"]
for name in names:
  if name[0] == 'M':
    continue
  print(name)
Riyas
  • 268
  • 2
  • 11
  • 1
    Friendly hint: This can be simplified (both code-wise and interpretation) by using the `!=` operator; thus removing the `continue` and printing when validated to True. – S3DEV Oct 30 '20 at 22:48
  • I appreciate the suggestions on this. I will check on this. – scottie1701 Nov 01 '20 at 01:30