简体   繁体   English

如何为每个打印的新行增加前导空格的数量?

[英]How can I increase the number of leading spaces for each new line printed?

I want to print the letter "H" 10 times (preferably in a loop) but for every new line that gets printed, the "H" has to move one space. 我想打印字母“H”10次(最好是循环)但是对于每个打印的新行,“H”必须移动一个空格。 How can I do this? 我怎样才能做到这一点?

Here is my original code: 这是我的原始代码:

for a in range(0,10):
    b=" "
    b+="  "
    print(b+"H");

You can accomplish this simply by multiplying the index by the number of spaces you want: 您只需将索引乘以所需的空格数即可完成此操作:

>>> for i in range(10):
    print i * " " + "H"

H
 H
  H
   H
    H
     H
      H
       H
        H
         H

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

相关问题 如何用新的印刷线替换印刷线? - How can I replace a printed line with a new printed line? 在解析之前,如何为字符串变量`interface_info` 中的每一行去除前缘空格? - How can leading edge spaces be stripped for each line in the string variable `interface_info` before parsing? 如何在for循环中将每行增加1或更多? - How do I increase each new line by 1 or more in a for loop? 如何增加python中打印的列数? - How to increase the printed number of columns in python? 对于 python 字符串,如何将每一行打印为新行? - for python string how can I print each line as a new line? 我应该如何使以下打印语句在一行的每一部分都使用一致数量的空格? - How should I make the following print statement take consistent number of spaces for each portions of a line? 如何获得逐行打印的telnet输出? - How can i get telnet output printed line by line? 如何在输入中忽略空格,但在最后打印? - How can spaces be ignored in the input, but printed at the end? 如何在前导零数中增加 int 数? - How can i increment int number in leading zero number? 我如何在新行上显示 pandas 中 start_date 和 end_date 之间每个月的天数? - how can i have on a new line the number of days for each month between start_date and end_date in pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM