python - Conditionally parsing a string -


i'm working on small script compile csv file.

i've come code combine string.

site = "{}.{}".format(subdomain, fulldomain) 

however there situation subdomain may not exist. if case, output ".domain.tld" not correct.

i wondering if there condition can add in format instruction above, or simpler check output , remove dot @ beginning if any.

thanks

how pretty straightforward one-liner?

"{}{}{}".format(subdomain, '.' if subdomain else '', fulldomain) 

and can name each format item like:

"{subdomain}{dot}{fulldomain}".format(subdomain=subdomain,                                        dot='.' if subdomain else '',                                        fulldomain=fulldomain) 

or, can go way:

"{}{}".format(subdomain + '.' if subdomain else '', fulldomain) 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo