gpt4 book ai didi

python - 在 python 3 中更改 .format 长度

转载 作者:行者123 更新时间:2023-12-02 07:18:31 25 4
gpt4 key购买 nike

我想更改代码中格式的长度,

symbolWanted = input('Input symbol wanted for triangle: ')
baseLength = int(input('Input base length of triangle(odd number): '))
for i in range (1,baseLength,2):
print(('{:^100}').format(i*symbolWanted))

有没有办法使用变量来更改格式的长度,例如

print(('{:^baseLength}').format(i*symbolWanted))

谢谢。

最佳答案

您可以在格式字符串中使用变量:

print(('{:^{bl}}').format(i*symbolWanted, bl=baseLength))

关于python - 在 python 3 中更改 .format 长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60109524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com