What is the result of % in Python?

前端 未结 19 1982
粉色の甜心
粉色の甜心 2020-11-22 00:57

What does the % in a calculation? I can\'t seem to work out what it does.

Does it work out a percent of the calculation for example: 4 % 2

19条回答
  •  执笔经年
    2020-11-22 01:06

    Somewhat off topic, the % is also used in string formatting operations like %= to substitute values into a string:

    >>> x = 'abc_%(key)s_'
    >>> x %= {'key':'value'}
    >>> x 
    'abc_value_'
    

    Again, off topic, but it seems to be a little documented feature which took me awhile to track down, and I thought it was related to Pythons modulo calculation for which this SO page ranks highly.

提交回复
热议问题