1

I've been seeing the % sign in python tutorials but they're not using it like modulo.

This is the code from Python app tutorial:

message = 'GIEWIVrGMTLIVrHIQS' #encrypted message
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

for key in range(len(LETTERS)):
   translated = ''
   for symbol in message:
      if symbol in LETTERS:
         num = LETTERS.find(symbol)
         num = num - key
         if num < 0:
            num = num + len(LETTERS)
         translated = translated + LETTERS[num]
      else:
         translated = translated + symbol
print('Hacking key #%s: %s' % (key, translated))

This line:

    print('Hacking key #%s: %s' % (key, translated))

I don't also know the # sign and the s, I am thinking they're regular expression but they didn't import regular expression

Meruem
  • 11
  • 1
  • These are comprehensively answered here https://stackoverflow.com/questions/4288973/whats-the-difference-between-s-and-d-in-python-string-formatting – Med Sep Nov 23 '20 at 12:38
  • Whatever tutorial you found this on is really old. If you're using Python 3 you should use the more modern string formatting options which are the `str.format()` method and `f-strings` – Tomerikoo Nov 23 '20 at 12:44

1 Answers1

1

# is an normal text. and %s means string format.
%s acts a placeholder for a string while %d acts as a placeholder for a number
You can use %s as putting str into str, and %d is int. you can contain int into str using this.

and if you do % (key, translated)
then that means putting multiple arguments for string format.

And now, use str.format() and for python ver 3.6↑ use f string format.

ppwater
  • 2,315
  • 4
  • 15
  • 29