Print a Character n Times in Python

In Python, the print() function is used to display some results on the output console. The print() function has evolved in Python. It started as a statement and it wasn’t until Python 3 that it was changed to a function.

We will discuss how to print a character n times in Python.

Using the * operator to print a character n times in Python

The * operator can be used in different ways with different objects in Python. We can use it to perform arithmetic multiplication, unpack elements, and now we will discuss how to use it to print a character n times in Python.

In the print() function we can specify the character to be printed. We can use the * operator to mention how many times we need to print this value.

See the code below.

Output:

aaaaa

In the above example, we printed the character five times using the * operator.

In case, if you are using Python 2.x, here is the syntax.

In case, you want to use separator such as _, here is syntax to use separator.

Output:

a_a_a_a_a

As you can see, repeating character is separated by -.

Using the for loop to print a character n times in Python

The range() function is used in Python to create a sequence between the provided range. We can iterate over this sequence with the for loop.

We can use the for loop to print a character n times in Python. For this, we will iterate over the sequence of required length and print the character in every iteration.

Output:

a
a
a
a
a

In the above example, we create a sequence using the range() function and print the character in every iteration of the sequence using the for loop. Note that the character is displayed in a new line in every iteration.

Conclusion

In this tutorial, we discussed how to print a character n times in Python. There are two methods for this. In the first method, we used the * operator in the print() function to print it the required times. We also discussed the use of the for loop to print a character n times in Python.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 25 June

    Convert Roman Number to Integer in Python

    Table of ContentsHow to Convert Roman Number to Integer in PythonHow to convert roman number to integer in python?Using the if...else statement to convert roman number to integer in Python.Using classes and a dictionary to convert roman number to integer in Python.Using the roman module to convert roman number to integer in Python.ConclusionWas this post […]

  • 11 June

    Remove Single Quotes from String in Python

    Table of ContentsRemove single quotes from string in Python.How to remove single quotes from string in Python?Using the replace() function to remove single quotes from string in Python.Using a for loop to remove single quotes from string in Python.Using the join() function to remove single quotes from string in Python.Using the re module functions to […]

  • 11 June

    Escape Backslash Character in Python

    Table of ContentsEscape backslash character in python.How to use escape backslash character in python?Using the escape backslash character in python to represent whitespace characters.Using the escape backslash character in python to turn special characters into ordinary characters.Using the escape backslash character in f-strings in Python.Using the escape backslash character in raw strings in Python.Was this […]

  • 11 June

    Break out of function in Python

    Table of ContentsBreak out of function in PythonHow to break out of function in Python?Using the return statement to break out of function in PythonUsing the try and except block to break out of function in PythonUsing the sys.exit() function to break out of function in PythonConclusionWas this post helpful? In this post, we will […]

  • 11 June

    Check if Object Is Iterable in Python

    Table of ContentsWhat are iterables in Python?How to check if object is iterable in Python?Using the iter() function to check if object is iterable in PythonUsing the for loop to check if object is iterable in PythonUsing the isinstance() function to check if object is iterable in PythonConclusionWas this post helpful? In this post, we […]

  • 11 June

    Save Numpy Array as Image in Python

    Table of ContentsSave numpy array as image in PythonHow to save numpy array as image in Python?Using the PIL library to save numpy array as image in PythonUsing the matplotlib.image library to save numpy array as image in PythonUsing the opencv library to save numpy array as image in PythonUsing the imageio library to save […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.