How many people knew this? | Sololearn: Learn to code for FREE!


How many people knew this?

I only found this out today. I knew how to use the end elements in the print function e.g: print("Hello","World",end="!") outputs Hello World!,but I never knew the end element stops the interpreter from jumping to the next line. Try this code: print("Hello",end = "") print("World") I thought it would print on two lines instead of 1

7/18/2018 6:13:38 PM

What does the Python say?

108 Answers

New Answer


I think I read it in some course, but forgot about that feature. Thanks :) If you think about it, it makes total sense. end = "something" just replaces the default end = "\n" (aka new line) ending of a print by somehting Same for sep and " " (space)


for i in range(5): print(i,end="") prints 01234 whereas, for i in range(5): print(i) prints 0 1 2 3 4


try this code


cool..πŸ˜πŸ˜ƒbut I knew thatπŸ˜ƒπŸ‘


i didn't knew thatπŸ˜… Thanks for info


Shuja Abrar No, end="" does not leave a space. The next print will be concatenated, with no space. end=" " will leave a space


try print("hello", "world", sep = "_")


Knew itπŸ˜„ But it will be helpful for others.πŸ‘πŸ‘


thanks for infoπŸ“• What does the Python say? This is helpfull !!!πŸ‘πŸ‘πŸ‘


Great 😊i didn't knew that πŸ˜ƒ


Great . I didn't know that . Thanx


One rejection isn't the end of the world, if you put your mind to it, you can acomplish anything :). Make it you can do it


end defines how print ends and newline ("\n") is default, sep defines how different arguments in print are separated and whitespace is default (" ") Type help(print) and run in compiler to see more! But strange SoloLearn Python 3 Tutorial never mentioned.


Thanks Matthias. Never thought of it in that way.


Thanks for the information about print("hello",end="") to supress the \n at end of the print. I always used sys.stdout.write(...) or sys.stdout.write(format % vals ) where format is the C format string and vals is a tuple of things to print. Note: you have to import sys to use sys.stdout.write . """ C's printf function """ def printf( format, *vals ): sys.stdout.write( format % vals ) By the way, does print("foo", end="") only work in python 3? in python 2.3 the code above get a syntax error. On my old laptop, I still have to use sys.stdout.write("foo") to supress the ending "\n" .


always had this thing in my mind but never bothered to find it out thanlks forr sharingg πŸ€—


Yes! i found out when i was researching for my histogram contest. i used it to draw histogram in this code πŸ˜πŸ‘‡πŸ‘‡


What does the Python say? yes all are set by default values as per the function definition.


I knew it :) end="" is useful when you don't want to go to new line. It is often needed to print more things on one line.


The nice thing is that you can put any character or piece of code after end= and it will effect the end of the print statement (this can work for sep= , too). So, if you still want a newline at the end of your statement, you can write like this: print("Hello", end="\n") print("World") ''' outputs: Hello World '''