简体   繁体   English

如何在同一行打印这些字符?

[英]How do I print these characters on the same line?

How would I get this function to repeat the same characters given from the user on the same line?我如何让这个函数在同一行重复用户给出的相同字符? Instead it prints it separately on each line.相反,它在每一行上单独打印。

character_to_enter = input('What character would you like to repeat?: ')
character_count = input("How many " + character_to_enter + "'s would you like to enter?: ")


def repeater(character_count):
    for repeats in range(int(character_count)):
        print(character_to_enter)


repeater(character_count)

The output looks like this with the current code above输出看起来像上面的当前代码

What character would you like to repeat?: f
How many f's would you like to enter?: 2
f
f

Process finished with exit code 0

I need it to look like this我需要它看起来像这样

What character would you like to repeat?: f
How many f's would you like to enter?: 4
ffff

Process finished with exit code 0

One of the easiest and simplest ways to achieve this, is to use the "end" argument of print.实现这一点的最简单和最简单的方法之一是使用 print 的“结束”参数。 Like so:像这样:

def repeater(character_count):
    for repeats in range(int(character_count)):
        print(character_to_enter, end="")


repeater(character_count)

This way instead of ending each print with the default newline character, it doesn't add any character.这种方式不是用默认的换行符结束每次打印,而是不添加任何字符。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM