yeongmin hong
yeongmin hong

Reputation: 17

How to use variable in url?

I'm trying to develop a maco in python. I need a method that changes the URI given in the input variables.

For instance, in www.(variable).com I need to change the URL www.1.com to www.2.com

Upvotes: 1

Views: 309

Answers (2)

Chris Doyle
Chris Doyle

Reputation: 11992

You could store your list of names in a list and then interpolate the variable into the f-string like:

names = ['google', 'amazon', 'microsoft']
for name in names:
    print(f"www.{name}.com")

OUTPUT

www.google.com
www.amazon.com
www.microsoft.com

Upvotes: 0

Tom Gebel
Tom Gebel

Reputation: 805

You can use formatted strings like this:

variable = 1
url = f"www.{variable}.com"

Upvotes: 1

Related Questions