How do I print a number n times in Python?
I can print 'A' 5 times like this:
print['A' * 5]
AAAAA
but not 10, like this:
print[10 * 5]
50
I want the answer to be 10 10 10 10 10
How do I escape the mathematical operation in print[]
?
I am not asking how to print the string '10' n times, I am asking how to print the number 10 n times.
asked May 11, 2019 at 15:49
Luther_BlissettLuther_Blissett
3051 gold badge7 silver badges13 bronze badges
4
10*5
actually does multiplication and ends up giving you 50, but when you do '10 '*5
, the *
operator performs the repetition operator, as you see in 'A' * 5
for example
print['10 ' * 5]
Output will be 10 10 10 10 10
Or you can explicitly convert the int to a string via str[num]
and perform the print operation
print[[str[10]+' '] * 5]
answered May 11, 2019 at 15:50
If you have the number as a variable:
number = 10
print["f{number} " * 5]
Or without f-strings:
number = 10
print[[str[number]] + ' '] * 5]
If you just want to print a number many times, just handle it as as string:
print["10 " * 5]
answered May 11, 2019 at 15:51
ruoholaruohola
20.1k6 gold badges55 silver badges86 bronze badges
This trick only works for strings.
print['10' * 5]
Will print:
1010101010
That's because the *
operator is overloaded for the str
class to
perform string repetition.
answered May 11, 2019 at 15:50
rdasrdas
19.4k6 gold badges32 silver badges43 bronze badges
How can I repeat a string multiple times, multiple times? I know I can use a for loop, but I would like to repeat a string x
times per row, over n
rows.
Nội dung chính
- How do you write multiple times in Python?
- How do you print your name 10 times in Python?
- How do you print multiple prints in Python?
- How do you print a name 5 times in Python?
For example, if the user enters 2
, the output would be:
@@
@@
@@
@@
Where x
equals 2, and n
equals 4.
Gino Mempin
20.8k24 gold badges84 silver badges111 bronze badges
asked Jun 9, 2011 at 13:20
5
If you want to print something = '@'
2 times in a line, you can write this:
print[something * 2]
If you want to print 4 lines of something, you can use a for loop:
for i in range[4]:
print[something]
Martin Thoma
113k148 gold badges570 silver badges873 bronze badges
answered Jun 9, 2011 at 13:38
SergSerg
5381 gold badge3 silver badges6 bronze badges
3
for i in range[3]:
print "Your text here"
Or
for i in range[3]:
print["Your text here"]
answered Jun 9, 2011 at 13:23
JABJAB
20.2k6 gold badges68 silver badges79 bronze badges
1
It amazes me that this simple answer did not occur in the previous answers.
In my viewpoint, the easiest way to print a string on multiple lines, is the following :
print["Random String \n" * 100]
, where 100 stands for the number of lines to be printed.
answered Oct 1, 2019 at 11:28
Timbus CalinTimbus Calin
12.4k4 gold badges36 silver badges54 bronze badges
So I take it if the user enters 2
, you want the output to be something like:
!!
!!
!!
!!
Correct?
To get that, you would need something like:
rows = 4
times_to_repeat = int[raw_input["How many times to repeat per row? "]
for i in range[rows]:
print "!" * times_to_repeat
That would result in:
How many times to repeat per row?
>> 4
!!!!
!!!!
!!!!
!!!!
I have not tested this, but it should run error free.
Sebastian
1,1956 silver badges18 bronze badges
answered Jun 10, 2011 at 1:08
Josh HuntJosh Hunt
13.9k26 gold badges76 silver badges98 bronze badges
EDIT: Old answer erased in response to updated question.
You just store the string in a variable:
separator = "!" * int[raw_input["Enter number: "]]
print separator
do_stuff[]
print separator
other_stuff[]
print separator
answered Jun 9, 2011 at 13:32
rows = int[input['How many stars in each row do you want?']]
columns = int[input['How many columns do you want?']]
i = 0
for i in range[columns]:
print ["*" * rows]
i = i + 1
Pretasoc
1,11012 silver badges20 bronze badges
answered Oct 12, 2018 at 9:56
1
The question is a bit unclear can't you just repeat the for loop?
a=[1,2,3]
for i in a:
print i
1
2
3
for i in a:
print i
1
2
3
answered Jun 9, 2011 at 13:24
Fredrik PihlFredrik Pihl
43.4k7 gold badges81 silver badges130 bronze badges
2
def repeat_char_rows_cols[char, rows, cols]:
return [char*cols + '\n']*rows
>>> print[repeat_char_rows_cols['@', 4, 2]]
@@
@@
@@
@@
answered Jun 10, 2011 at 17:32
tzottzot
89.1k29 gold badges136 silver badges201 bronze badges
For example if you want to repeat a word called "HELP" for 1000 times the following is the best way.
word = ['HELP']
repeat = 1000 * word
Then you will get the list of 1000 words and make that into a data frame if you want by using following command
word_data =pd.DataFrame[repeat]
word_data.columns = ['list_of_words'] #To change the column name
answered May 29, 2015 at 12:13
surendrasurendra
212 silver badges5 bronze badges
n=int[input[]]
w=input[]
print[f'{w}\n'*n]
# this will print the word[w] , n times in separte lines
answered Jun 26, 2021 at 4:22
How do you write multiple times in Python?
How to Repeat a String Multiple Times in Python.
Problem Formulation and Solution Overview..
Method 1: Use print[] and multiplication operator..
Method 2: Use a For Loop and range[].
Method 3: Use the input[] function..
Method 4: Use itertools.repeat[].
Method 5: Use a DataFrame..
Summary..
Regex Humor..
How do you print your name 10 times in Python?
Example: count = 0; while count < 10: print["My name is Vidyut"] count += 1 else: print[“String is printed ten times!”]
How do you print multiple prints in Python?
To print multiple variables in Python, use the print[] function. The print[*objects] is a built-in Python function that takes the *objects as multiple arguments to print each argument separated by a space.
How do you print a name 5 times in Python?
Solution:.
Here comes the program..
Using loop. for i in range[5]: print["My name is abcd."].
Without using loop. print["My name is abcd.\n"*5] When strings are multiplied with any number [n] , the new string formed becomes the original string repeated n times..