Strip last period from a host string

10,184

You do it like this:

hostname.rstrip('.')

where hostname is the string containing the domain name.

>>> 'domain.com'.rstrip('.')
'domain.com'
>>> 'domain.com.'.rstrip('.')
'domain.com'
Share:
10,184
Simply  Seth
Author by

Simply Seth

Updated on July 30, 2022

Comments

  • Simply  Seth
    Simply Seth almost 2 years

    I am having a hard time figuring out how to strip the last period from a hostname ...

    current output:

    • domain.com.
    • suddomain.com.
    • domain.com.
    • subdomain.subdomain.com.
    • subdomain.com.

    desired output:

    • domain.com
    • subdomain.com
    • domain.com
    • subdomain.subdomain.com

    attempt 1:

    print string[:-1]  #it works on some lines but not all
    

    attempt 2:

     str = string.split('.')
     subd = '.'.join(str[0:-1])
     print subd    # does not work at all 
    

    code:

    global DOMAINS
    
    if len(DOMAINS) >= 1:
      for domain in DOMAINS:
        cmd = "dig @adonis.dc1.domain.com axfr %s |grep NS |awk '{print $1}'|sort -u |grep -v '^%s.$'" % (domain,domain)
        p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,   shell=True)
        string = p.stdout.read()
        string = string.strip().replace(' ','')
        if string:
          print string
    
  • Oscar Mederos
    Oscar Mederos about 11 years
    @isedev gave you the answer you were looking for (in fact, the simplest one). that should be the accepted answer.