Use of modulo in mathematical functions as well as strings


#1

Hello

I am trying to understand the % operator fully.

It is used in mathematical functions to return the remainder of a division. i.e

5 % 2
1

It is also used to "inject" text into strings. i.e

string_1 = "Camelot"
string_2 = "place"
print "Let's not go to %s. 'Tis a silly %s." % (string_1, string_2)
Let's not go to Camelot. 'Tis a silly place.

I was wondering if the % operator, at the lower level, carries out different functions depending on whether it's interacting with a mathematical function or a string?
Or whether it does the same thing regardless of whether it's interacting with a mathematical function or a string, and there's some underlying functionality/concept which allows the operator to treat both scenarios the same way and return the expected result regardless?

My apologies if the question is hard to understand, I don't know how to ask this in simpler terms


#2

the % works differently depending of its part of a string or that you use it in a mathematical way.

Python is written in C, so the C code is written in such a way, that C knows how to deal with %


#4

yes, exactly :slight_smile:


#5