Python: форматирование строки с включением пробелов перед напечатанным оператором - PullRequest
0 голосов
/ 13 марта 2020

Как это сделать, чтобы в начале второй половины следующего оператора печати было 10 пробелов?

print("I need ten spaces", "This sentence should have ten spaces in between this and the first statement")

Я бы хотел, чтобы он вывел ...

I need ten spaces          This sentence should have ten spaces in between this and the first statement

Ответы [ 3 ]

6 голосов
/ 13 марта 2020

Используйте аргумент sep в print:

print("I need ten spaces", 
      "This sentence should have ten spaces in between this and the first statement", 
      sep=' '*10)
# I need ten spaces          This sentence should have ten spaces in between this and the first statement
1 голос
/ 13 марта 2020

Каждый оператор печати принимает необязательный параметр с именем sep. sep по умолчанию представляет собой один пробел, поэтому, как правило, между каждой строкой стоит один пробел, разделенный запятыми. Вот как изменить это на 10 пробелов:

print("I need ten spaces", "This sentence should have ten spaces in between this and the first statement", sep="          ")

или более просто:

print("I need ten spaces", "This sentence should have ten spaces in between this and the first statement", sep=" " * 10)
1 голос
/ 13 марта 2020

Вы можете умножить пробелы

print("I need ten spaces", " " * 8, "This sentence should have ten spaces in between this and the first statement")

или что-то вроде

print("I need ten spaces{0}This sentence should have ten spaces in between this and the first statement".format(" "*10))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...