-3

I am using API documentation with the following snippet:

    r = requests.get("%slogin?username=%s&password=%s"
                     % (BASEURL, username, password))

What is the % symbol doing here? I have never passed this into .get before and I am not sure what it is doing.

cwfmoore
  • 225
  • 3
  • 11

2 Answers2

2

From this String formatting: % vs. .format vs. f-string literal

  • Python <2.6: "Hello %s" % name
  • Python 2.6+: "Hello {}".format(name) (uses str.format)
  • Python 3.6+: f"{name}" (uses f-strings)

it is there for formatting the string url

Hi computer
  • 946
  • 4
  • 8
  • 19
1

This is one way to do string formatting in Python. It doesn't have anything specifically to do with the get() function.

The first %s takes on the value of the first variable BASEURL. The second %s takes on the value of the second variable username, and so on.

See https://www.programiz.com/python-programming/string-interpolation for more examples.

John Gordon
  • 29,573
  • 7
  • 33
  • 58