This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Python, Printing multiple times,
I'd like to know how to print a string such as "String" 500 hundred times?
You can use repetition (*):
print('String' * 500)
In this way Python will first create the whole string ("StringStr...String") in memory and only then will print it.
If you don't want to use so much memory, then you can use a for loop:
for i in range(500):
print('String', end='')
print()
end='' is needed to prevent Python from printing end-of-line characters after each print. print() at the end is needed to print the end-of-line character after the last print.
Related
This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 1 year ago.
Is there a way how to remove the property of the print() function in Python that makes end of line after outputting its argument? Or is there a different function to do the output of argument but no the end line?
To illustrate this, lets say I want to output the English alphabet on one line. The following code prints every letter on a new line:
for x in range(26):
print(chr(x+97))
While the second code prints it the way requested, but by concatenation of strings instead:
s=''
for x in range(26):
s+=chr(x+97)
print(s)
print(“Hello”, end=“”)
print(1)
Output: Hello1
Hope this helps :) the default value of end is “\n” and you can change it to anything you want.
This question already has an answer here:
Why does printing a tuple (list, dict, etc.) in Python double the backslashes?
(1 answer)
Closed 1 year ago.
enter image description here
is there a way to print single backslash within list?
Regarding the first version of your question, I wrote this:
First, this expression x='\' isn't right in Python in python. you should rather puth it this way: x='\\', since back slash is a special character in python.
Second, try this:
l=['\\'] print(l)
This will print: ['\\']
But when you execute this: print(l[0]), it renders this '\'. So basically, this ['\\'] is the way to print a backslash within a list.
This question already has answers here:
How do I split the definition of a long string over multiple lines?
(30 answers)
Closed 5 years ago.
I need to print a long const sentence in a python script and we follow the "not more than 80 chars a line" rule.
I know the following would work but is there a better way to write this?
print ("This is a reaaaaaaaaaaaaaaaaaaaaaaaally long "
"sentencexxxxxxxxxxxxxxxxxxxxxxxxxxxx").format("")
or,
print ("This is a reaaaaaaaaaaaaaaaaaaaaaaaally long " +
"sentencexxxxxxxxxxxxxxxxxxxxxxxxxxxx")
Or, if one of them is preferable than the other one.
Edit:To make it more clear, the expected output is,
This is a reaaaaaaaaaaaaaaaaaaaaaaaally long sentencexxxxxxxxxxxxxxxxxxxxxxxxxxxx
I think this is a better way:
print(
"This is a reaaaaaaaaaaaaaaaaaaaaaaaally long "
"sentence"
)
Actually, you need nothing to concatenate strings.
This question already has answers here:
Why is semicolon allowed in this Python snippet?
(15 answers)
Closed 6 years ago.
What's the difference with Python statements ending with ; and those does not?
There is really no difference. Python ends a line of code at the end of the logical line, or when it encounters ;
The only advantage to using ; is that you can stack multiple logical lines into one physical line. For example (in python3):
import sys
for i in range(10):
print(i, end=' '); sys.stdout.flush()
That said, this is terrible coding style, so don't ever do it
Semicolons serve the same purpose as the newline character. It is really just bad style to use a semicolon, often from people coming from languages where lines require it.
This question already has answers here:
How to change a string into uppercase?
(8 answers)
Closed 9 years ago.
I created a .txt file with random words that I'm importing into my script. I want then to take all the words, print one on each line and convert them all to uppercase. I have the first part done:
a=open("redchief.txt").read().split()
print ' \n'.join(a)
I'm having problems converting the data into capital letters. Here is some of the data:
It looked like a good thing: but wait till I tell you.
Just change your last line from:
print ' \n'.join(a)
to:
print ' \n'.join(a).upper()
you don't have to store the result in a separate variable first, since ' \n'.join(a) gives you a string object whose upper() method you can call.