Question

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?

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top