简体   繁体   English

Python更改输出格式

[英]Python change output format

I created this program and I need it to output like this: 我创建了这个程序,我需要它像这样输出:
这个

How can I do that? 我怎样才能做到这一点?

n=int(input(""))
L = []
x=0
c=0
while x<=n-1:
    Numero=int(input(""))
    final="*"*Numero,Numero
    L.append(final)
    x=x+1
for elem in L:
        print(elem)

elem is a tuple of a string and an integer number. elem是字符串和整数的元组。 There are several ways to display it as a string: 有几种方法可以将其显示为字符串:

print(*elem)

print("%s %d" % elem)

print("{} {}".format(*elem))

print(elem[0], elem[1])

You can use format() to append strings: 您可以使用format()附加字符串:

n=int(input("Please input number"))
r=["*"*int(input("")) for i in range(n)]
for i in r:
    print("{} {}".format(i,len(i)))

Output: 输出:

Please input number4
4
2
3
1
**** 4
** 2
*** 3
* 1

You want to unpack the arguments to the function using * . 您想使用*解压缩函数的参数。 So your print statement would become: 因此,您的打印报表将变为:

print(*elem)

See the python tutorial for more information. 有关更多信息,请参见python教程

>>> e = ('****', 4)
>>>> print(e)
('****', 4)
>>> print(*e)
**** 4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM