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.
Related
This question already has answers here:
Printing a function in Python
(5 answers)
Closed 3 years ago.
I'm trying to write program, who's printout specific words (without "e"). But i have a problems.
that's my code:
def has_no_e(fin,word):
fin = open('words.txt')
for line in fin:
word = line.strip()
if 'e' not in word:
print(word)
else:
continue
print(has_no_e)
Pycharm after run it printout that:
function has_no_e at 0x00E078A0
I don't know what's wrong. Thanks everybody for any help.
Try has_no_e(<fin>, <word>), without the print. What you're doing there is printing the function address itself, if you try doing has_no_e(<fin>, <word>) the function contents will be executed instead, which is what you want.
P.S. Replace fin and word with the actual parameters you want to pass.
This question already has answers here:
why is python string split() not splitting
(3 answers)
Closed 6 years ago.
I've got a simple block of code which is meant to iterate over a list of strings and split each item in the list into a new list, then call another function on each item:
list_input = take_input()
for item in list_input:
item.split()
system_output(item)
The problem is that 'item.split()' doesn't seem to be doing anything. With a print(item) statement in the penultimate line, all that is printed to the console is the contents of item, not the contents split into a new list. I feel like I'm missing something obvious, can anyone help? Thanks!
EDIT: So I've been informed that strings are immutable in Python, and in light of this replaced the 'item.split()' line with 'item = item.split()'. However, I am still running into the same error, even with item redefined as a new variable.
split() does not split the string inplace, it only returns a splitted string that you have to put in an other variable.
This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 6 years ago.
I tried to get the output of python as
def main():
i=raw_input()
a=input()
print(i*a)
main()
input:hello,
2.
output: "hellohello"
but i am getting output as
output:" hello
hello "
.how can i get the two strings as output continuously in same line from one print statement.for example:"hellohello "
To remove the white space from the single execution of print. Use .strip() function as (as I believe you have white space within your i string):
print(i.strip()*a)
But if you want all the executions of print in single line, you should use end argument of print to define empty string. Your print statement should be like:
print(i.strip()*a, end='')
based on the fact that you are using raw_input I'm assuming you are using python 2. Therefor you could just change the print() with a print and your code should work
def main():
i=raw_input()
a=input()
print i*a <---
main()
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.
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.