简体   繁体   English

我怎样才能让这个 function 根据我指定的数字在多行(每行一个单词)中打印字符串?

[英]How can I make this function print the string in multiple lines (one word in each line) based on the number I specify?

This is my code, I want the output to be printed down in multiple lines not just one.这是我的代码,我希望将 output 打印成多行,而不仅仅是一行。

def words(name, num):
    for i in range(num):
        print(name)
        
print("content-type: text/html\n")

words("Max", 2)

The output is: output 是:

Max Max

I want我想

Max
Max 

I'm running this through HTTP.我正在通过 HTTP 运行它。

You have to either wrap each word in a HTML paragraph element or put a line break after each word.您必须将每个单词包装在 HTML 段落元素中,或者在每个单词后放置一个换行符。 This is needed because browsers remove simple newlines ( \n ).这是必需的,因为浏览器会删除简单的换行符( \n )。 They need special HTML tags for a line break.他们需要特殊的 HTML 标签来换行。 In a pragraph:在一段中:

def words(name, num):
    for i in range(num):
        print("<p>" + name "</p>")
        
print("content-type: text/html")

words("Max", 2)

With linebreak after the word:在单词后加上换行符:

def words(name, num):
    for i in range(num):
        print(name + "<br>")
        
print("content-type: text/html\n")

words("Max", 2)

The print function takes an end parameter which can be specified as: print function 带有一个end参数,可以指定为:

print(name, end='<br>')

Try this:尝试这个:

def words(name, num):
    for i in range(num):
        print(name, "\n")

暂无
暂无

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

相关问题 对于 python 字符串,如何将每一行打印为新行? - for python string how can I print each line as a new line? 如何使用随机会话号打印多行文本? - How can I print multiple lines of text with random session number? 如何打印多行并使每条输出行不同? - How to print multiple lines and make each output line different? 我的 function 如何打印出“#”行。 参数是一个整数列表,它们指定每行应该包含多少个“#” - How can my function print out lines of "#". The argument is a list of integers and they specify how many "#" each line should contain 如何打印字符串中每个字母出现的次数? - How can i print number of times each alphabet occurs in the string? 如何在 Python 中将多行字符串合并为一行,并用空格分隔它们? - How can I combine multiple lines of string into one line in Python with a space to separate them? 如何在各行上打印列表的每个元素,在python中以行号开头? - How do I print each element of a list on individual lines, preceded by the line number in python? 如何打印多行,然后让后续行分别替换每行(Python) - How can I print multiple lines, having subsequent lines replacing each of them independently (Python) 我如何打印这两个字符串,一个在一行,另一个在另一个? - How can i print these 2 string, one in a line and the other on another? 如何将行写入 NamedTemporaryFile 然后打印每一行 - how do I write lines to a NamedTemporaryFile then print each line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM