Python Forum
what does % stand for in an expression ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what does % stand for in an expression ?
#1
Hi
I found the following example in the python doc :

from typing import NamedTuple import re class Token(NamedTuple): type: str value: str line: int column: int def tokenize(code): keywords = {'IF', 'THEN', 'ENDIF', 'FOR', 'NEXT', 'GOSUB', 'RETURN'} token_specification = [ ('NUMBER', r'\d+(\.\d*)?'), # Integer or decimal number ('ASSIGN', r':='), # Assignment operator ('END', r';'), # Statement terminator ('ID', r'[A-Za-z]+'), # Identifiers ('OP', r'[+\-*/]'), # Arithmetic operators ('NEWLINE', r'\n'), # Line endings ('SKIP', r'[ \t]+'), # Skip over spaces and tabs ('MISMATCH', r'.'), # Any other character ] tok_regex = '|'.join('(?P<%s>%s)' % pair for pair in token_specification) line_num = 1 line_start = 0 for mo in re.finditer(tok_regex, code): kind = mo.lastgroup value = mo.group() column = mo.start() - line_start if kind == 'NUMBER': value = float(value) if '.' in value else int(value) elif kind == 'ID' and value in keywords: kind = value elif kind == 'NEWLINE': line_start = mo.end() line_num += 1 continue elif kind == 'SKIP': continue elif kind == 'MISMATCH': raise RuntimeError(f'{value!r} unexpected on line {line_num}') yield Token(kind, value, line_num, column) statements = ''' IF quantity THEN total := total + price * quantity; tax := price * 0.05; ENDIF; ''' for token in tokenize(statements): print(token)
Line 22 reads
 tok_regex = '|'.join('(?P<%s>%s)' % pair for pair in token_specification)
I suppose that each %s stands for the corresponding element of pair and that % stands for the tuple itself.

Where can I find any explanation of this coding ?

Thank you for having read my post.

Arbiel
using Ubuntu 22.04.5 LTS, Python 3.10.12
having substituted «https://www.lilo.org/fr/» to google, «https://protonmail.com/» to any other unsafe mail service and bépo to azerty (french keyboard layouts)
Reply
#2
Check the docs: Old-style string formatting

Also https://docs.python.org/3/library/stdtyp...formatting

Note, there are newer/better/powerful string formatting methods
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Sep-19-2025, 03:22 PM)buran Wrote: Check the docs: Old-style string formatting

Also https://docs.python.org/3/library/stdtyp...formatting

Note, there are newer/better/powerful string formatting methods

OK

Understood.

Thanks

Arbiel
using Ubuntu 22.04.5 LTS, Python 3.10.12
having substituted «https://www.lilo.org/fr/» to google, «https://protonmail.com/» to any other unsafe mail service and bépo to azerty (french keyboard layouts)
Reply
#4
In Python, the % operator is the modulo operator, it returns the remainder when the left operand is divided by the right operand. For more: Python Forum
Reply
#5
Generally speaking
x % y
is equivalent to
x.__mod__(y)
if the type of x implements a __mod__() method. Otherwise it is equivalent to
y.__rmod__(x)
if the type of y implements a __rmod__() method. Otherwise it raises a TypeError.

The effect and result depends on what these methods do, which can be arbitrary.
DeaD_EyE likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pass results of expression to another expression cmdr_eggplant 2 3,729 Mar-26-2020, 06:59 AM
Last Post: ndc85430
  Stand Alone Python App PadmaDavid 2 3,210 Feb-28-2020, 03:15 PM
Last Post: PadmaDavid
  how to compile a stand alone executable on linux? JackDinn 4 9,441 Mar-11-2018, 10:22 PM
Last Post: JackDinn

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.