meskerem meskerem - 1 year ago 51
Python Question

Is it possible to declare a variable with a value for string and a placeholder in python?

I am trying to initialize a long string value to a variable, but this string has a word that can not be constant, like this example.

Say I want to store a string like this.

str = ""

But assume the number 7833397 will change over time, so I am trying to find a way to store the string while making making a wildcard for the number. But I am not sure if this can be done in Python

Answer Source

Use the format method.

template = "{0}/meskerem"

# Lots of stuff happens here

url = template.format("7833397")

The format method supports its own little mini language, and depending on your use-case you may find it more intuitive to name the various parts of your template, too:

template = "{id}/{username}"

# Lots of stuff happens here

url = template.format(id="7833397", username="meskerem")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download