mLstudent33
mLstudent33

Reputation: 1175

Python index with s, d, b, c, o, x, X, n, etc... what are they?

I did not realize that Python indexes can have characters and not just integers.

I was looking at this Tensorflow tutorial for RNN text generation: RNN text generation TF 2.0

In the second cell after section title "Process the Text" it has this code:

print('{')
for char,_ in zip(char2idx, range(20)):
    print('  {:4s}: {:3d},'.format(repr(char), char2idx[char]))
    print('  {:4s}: {:2d},'.format(repr(char), char2idx[char]))#repr compute official string rep of an object
print('  ...\n}')

I played around with the integers in front of s and d and noticed they have something to do with spacing and alignment. But I came up empty handed on my search for the definitions of how and when to use these. Can somebody point me in the right direction?

Upvotes: 0

Views: 304

Answers (1)

wjandrea
wjandrea

Reputation: 33145

Those aren't indexes, they're format specifiers. s means "string" and d means "decimal number". The numbers in front indicate how much whitespace to use for alignment. These are defined in the Format Specification Mini-Language. The letters are "type"s and the numbers are "width"s.

Upvotes: 1

Related Questions