What is the meaning of Percent sign in Python?

Posted on

Question :

What is the meaning of Percent sign in Python?

What is the meaning of the percent sign in Python? for example :
The part of this Code ‘j%i%im%i’%(b,c,d) ?

>>> while d < len(r[0][b][c]):            
>>>      if r[0][b][c][d] >= 0 and e == 0:                                          
>>>         C.append({'operation':'j%i%im%i'%(b,c,d),'energie':r[1][b][c][d]},) 
>>>         e = 1 
Asked By: Daniel

||

Answer #1:

The ‘%’ symbol has a lot of different meanings.

In this case, it’s refererring to string formatting:
https://mkaz.com/2012/10/10/python-string-format/

Basically, it allows you to substitute a placeholder in the string with a new value.

>>> 'a%s' %('b')   # adding string
'ab'
>>> 'a%i' %(1)     # adding integer
'a1'
>>> 'a%d' %(1)     # adding integer (another form) 
'a1'

However, for integers, % has an entirely different meaning and can return a remainder from integer division (5%2 == 1).

Answered By: Alexander Huszagh

Leave a Reply

Your email address will not be published. Required fields are marked *