2

I understand the use of the modulus in regularity, but does it have multiple uses? For instance, I saw this:

print "key is '%s'" %keydecrypt
print "encrypted text is '%s'" %cipher

I don't understand what the modulus in the strings and then the modulus next to the variables are necessarily doing. -->is it a way to substitute values into string?

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Caddiana Runes
  • 121
  • 1
  • 2
  • 5
  • 1
    It's used for string formatting. You might want to look that up. – Achrome Dec 13 '13 at 04:10
  • Yes, it is. It's similar to `printf`-style formats in `C` code. The `%s` code means "apply `str()` to the argument, and insert the result in place of `%s`". – Tim Peters Dec 13 '13 at 04:13

2 Answers2

2

It is a text placeholder, In this case it substitutes what's in keydecrypt and cipher to %s in the string. So if keydecrypt and cipher were abc, output would be:

key is 'abc'
encrypted text is 'abc'

However the new string.format() function is more suitable as it is more explicit:

print "key is '{0}'" .format(keydecrypt)
print "encrypted text is '{0}'".format(cipher)
K DawG
  • 13,287
  • 9
  • 35
  • 66
1

Yes. It substitutes in the variables. It also can do more in regards to formatting.

Also, You don't need the apostrophes.

And you can also use multiple variables by passing a list. i.e;

print "key is: %s, encrypted text is: %s" % (keydecrypt, cipher)

And there are different types besides %s, like %r;

try:
    something
except Exception, e:
    print "The formatted Exception is: %r" % ex

It is generally better to use this than the other way;

print "This is my var: " + variable

Remember, in python, everything is an object.

Ross
  • 1,013
  • 14
  • 32