user3763437
user3763437

Reputation: 359

Modulus calculation in print function?

What I am missing here?

print('i % 100 = %d ' % (i % 100))

gives me: ValueError: unsupported format character ' ' (0x20) at index 7

a = i % 100
print('i % 100 = %d ' % (a))

Same error again.

Upvotes: 2

Views: 133

Answers (1)

arshajii
arshajii

Reputation: 129537

You have to escape % in the format string (using another %):

print('i %% 100 = %d ' % (i % 100))

In general, the character after an unescaped % is treated as a format specifier, and a space is an invalid specifier, hence the error.

Upvotes: 2

Related Questions