TransWikia.com

Необходимо вывести 111, а программа выводит ['1', '1', '1']

Stack Overflow на русском Asked on January 1, 2021

f = [1,1,1]
print(f)    

Очень важная универсальность программы, так как f = [1,1,1] не всегда принимает это значение.

3 Answers

Если речь идет только о выводе, уместно вспомнить функцию print():

Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

Итого, вся программа такая:

f = [1,1,1]
print(*f, sep="")

Работает!

Answered by Vasyl Kolomiets on January 1, 2021

f = [1,1,1]
a=''
for i in f:
    a+=str(i)
print(a)

Answered by Hayk Sardaryan on January 1, 2021

Используйте str.join:

f = [1,1,1]
print(''.join([str(i) for i in f]))

Answered by timur on January 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP