Python的ljust填充b‘’
ljust方法可以用来在字符串的右侧填充指定的字符,例如:
s = 'hello'
s = s.ljust(10, '*')
print(s)
输出结果为:
hello*****
在这个例子中,字符串s被填充到长度为10,使用字符''进行填充。如果想要使用字节串b''进行填充,则可以将字符串s转化为字节串,然后再进行填充。例如:
s = 'hello'
s = s.encode('utf-8').ljust(10, b'*')
print(s)
输出结果为:
b'hello*****'
在这个例子中,首先将字符串s使用encode方法转化为字节串,然后再进行填充。
原文地址: http://www.cveoy.top/t/topic/6fv 著作权归作者所有。请勿转载和采集!