如何生成长度为16的倍数的文本?

2024-09-28 16:53:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有不同长度的弦,例如:

text_1 = 'example'
text_2 = 'a'
text_3 = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Proin nibh augue, suscipit a, scelerisque sed, lacinia in, mi.'

我必须给它们加上chars(any),因为文本的长度必须是16的倍数。在

我需要它来做这个:

^{pr2}$

我该怎么做?在


Tags: textexampleipsumloremdolorametsitelit
2条回答

加上16和模的差

text_1 += ((16 - len(text_1) % 16)*'X')

这应该做到:

>>> s = 'fooo'
>>> s += ' ' * (16 - (len(s) % 16))
>>> s
'fooo            '
>>> len(s)
16

这里需要的字符数是16 - (len(s) % 16),因为len(s) % 16是长度除以16后剩下的字符数。在

相关问题 更多 >