2

I am working on a program that checks hostnames of specific sites, and I want to be able to insure that when asked for the hostname (with raw_input) it ends in a TLD (.com, .net, .org). I am not exactly sure how to do this in Python.

In bash I have:

local TLD=(com info org net)    
for entry in ${TLD[@]}; do
   blah blah    
done

What is the equivalent in Python?

Geoff
  • 7,935
  • 3
  • 35
  • 43

1 Answers1

4

endswith(suffix[, start[, end]]) will do the trick. Documentation

Please also note that suffix can be a tuple of suffices!

TLD = ('.com', '.info', '.org', '.net')
if raw_input("Please enter a hostname").endswith(TLD):
    # blah blah
jaynp
  • 3,275
  • 4
  • 30
  • 43
  • +1. Nice answer. He'll probably need to save the `raw_input` though. – Geoff May 29 '13 at 16:27
  • 1
    You might also consider using `.lower().endswith(TLD)` to allow for UPPER CASE. – Geoff May 29 '13 at 16:28
  • Geoff is correct, I do in fact want to store raw_input. I attempted to do that with a tmpvar list, but I keep getting broken conditionals. What would be the best way to capture that input, if it doesn't match the TLD, have it ask again, otherwise when the input is correct have it continue on with the program? I attempted to do a while True: loop with a list for the variable but I can't break the loop. –  May 29 '13 at 18:47
  • @Jacob What do you mean by broken conditionals? Do you want to keep store all of the non-matching TLD inputs as well? – jaynp May 29 '13 at 20:26
  • @user1850672 I asked a secondary question and was given the answer :) http://stackoverflow.com/questions/16822171/python-code-to-determine-if-tld-exists-prompt-again-if-not –  May 29 '13 at 20:44