0

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
Avenius
  • 21
  • 7
  • 1
    Try: `print(character_to_enter, end="")` – Dani Mesejo Dec 02 '19 at 17:59
  • Does this answer your question? [multiple prints on the same line in Python](https://stackoverflow.com/questions/5598181/multiple-prints-on-the-same-line-in-python) – kaya3 Dec 02 '19 at 18:15

1 Answers1

1

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.

Márcio Coelho
  • 333
  • 3
  • 11