简体   繁体   中英

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. 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:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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