简体   繁体   English

如何打印多行并使每条输出行不同?

[英]How to print multiple lines and make each output line different?

I want to make a script where i can print multiple lines (was thinking with a for or while loop) and every line that outputs is different like: 我想制作一个脚本,在其中我可以打印多行(正在考虑for或while循环),并且输出的每一行都是不同的,例如:

Output:

Hello.1
Hello.2
Hello.3

Is it posible to make with python? 可以用python制作吗?

I am afraid of answering this simple question but here is the answer using a for loop 我害怕回答这个简单的问题,但这是使用for循环的答案

for i in range(1,4):
    print ("Hello.%s" %i)

# Hello.1
# Hello.2
# Hello.3    

Second alternative way is using format 第二种替代方法是使用format

for i in range(1,4):
    print ("Hello.{}".format(i))

Third alternate is using f -strings as 第三选择是使用f -strings作为

for i in range(1,4):
    print (f"Hello.{i}")

If you have Python 3.6 or above, you can do it like this: 如果您具有Python 3.6或更高版本,则可以这样操作:

def nprint(n):
    for i in range(n):
        print(f"Hello.{i+1}")

nprint(3)

output:
Hello.1
Hello.2
Hello.3

在此处输入图片说明

暂无
暂无

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

相关问题 如何在多行中打印输出 - how to print the output in multiple lines 我怎样才能让这个 function 根据我指定的数字在多行(每行一个单词)中打印字符串? - How can I make this function print the string in multiple lines (one word in each line) based on the number I specify? 如何使用line.strip()打印多行 - How to print multiple lines with line.strip() 如何从多行打印到一行 - How to print to one line from multiple lines 如何显示具有多行不同功能的输出以相邻打印? - How do you display outputs with multiple lines of different functions to print next to each other? 如何在不同的行中为不同的测试用例打印 output? - How to print output for different test cases in different lines? 如何使用带有列表的字典将来自多个文件的输入合成并打印到输出中的每个键的一行中? - How to use dictionaries with a list to synthesize and print inputs from multiple files into a single line for each key in output? 使两个函数并行运行打印 output 到不同的行 - Make two functions run parallely print output to different line 如何将行写入 NamedTemporaryFile 然后打印每一行 - how do I write lines to a NamedTemporaryFile then print each line 我正在尝试一次打印多行,每行在 python 2.7.11 中都有一个设定速度的延迟打印 - I am trying to print multiple lines at once, with each line having a delayed print of a set speed in python 2.7.11
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM