-1

I'm learning multithreading in python and I was reading through this answer. I understand most of the code however there is this one line which I simply don't understand and I don't know how to search for it on Google as the '%' sign keeps returning modulo.

req.headers['Range'] = 'bytes=%s-%s' % (start, start+chunk_size)

I thought that req.headers['Range'] would retrieve some 'range' element from an array however here they are assigning it a value of 'bytes=%s-%s' % (start, start+chunk_size). I really just don't understand what is going on in this line. Things like 'bytes=%s-%s' I am assuming is some sort of python syntax which I am unaware of. If you could explain each term in this line that would be very much appreciated.

aaaakshat
  • 809
  • 10
  • 19

1 Answers1

1

In python there are multiple ways to format strings. using %s inside a string and then a % after the string followed by a tuple (or a single value), allows you to create a new string:

x = 5
y = 8
'my favourite number is %s, but I hate the number %s' % (x, y)

results in:

'my favourite number is 5, but I hate the number 8'

I think they call it C-type string formatting. For more information, you can check out this page.

In my opinion, it is easier to format string using f'strings, or .format(). Check out this page too

KenHBS
  • 6,756
  • 6
  • 37
  • 52
  • Thank you this helped alot! 'C-type string formatting' is the search term i needed to research this further. – aaaakshat Dec 27 '18 at 15:08