Printing strings in same line [duplicate] - python

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()

Related

What purpose of a "return" in this case? [duplicate]

This question already has answers here:
return, return None, and no return at all?
(5 answers)
What is the purpose of the return statement? How is it different from printing?
(15 answers)
is it possible to not return anything from a function in python?
(6 answers)
Closed 4 months ago.
I have a simple function which converts combination of characters to emojis. I don't understand completely a purpose of a "return" function ? Below i provide a piece of code in which im experimenting.
def emoji_converter(message):
words = message.split(" ")
emojis = {
":)": "😊",
":(": "πŸ˜”"
}
output = ""
for word in words:
output += emojis.get(word, word) + ' '
return output
print(emoji_converter("today is a good day :)"))
I want to figure out why without "return" after cycle "for" eventually while running a program i receive a value "None" and how which alternative way i can replace a "return" function?
print will output whatever the argument passed to it evaluates to. In this case, you're passing it a function invocation (i.e. "calling" the function), so it will print whatever is returned from the function.
If you omit the return, then there is nothing to print. So in this case, you cannot replace the return and expect it to print the resulting output.

Outputting argument but no new line feature in Python [duplicate]

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.

Python text.replace for removing section of a string [duplicate]

This question already has answers here:
Remove all whitespace in a string
(14 answers)
Closed 2 years ago.
I would like to see how I could remove a certain section of a string without leaving the blank space.
When running this code it outputs nice today, the problem is that I am replacing the characters with a blank space. Is there a fix to this or should I be using a completely different method entirely?
text = ("hello bye there ")
print(text.replace("hello", "nice").replace("there","today").replace("bye", ""))
you should remove white space at first:
text = ("hello bye there ")
print(text.replace("hello", "nice").replace("there","today").replace(" ", '').replace("bye" , " "))
the output will be:
nice today

Selective printout from file [duplicate]

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.

Python - Print a string a certain number of times [duplicate]

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.

Categories

Resources