简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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