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

+206

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

+97

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)

+56

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

+36

try this code https://code.sololearn.com/cQjfP8HJ0B4K/?ref=app

+23

cool..😁😃but I knew that😃👍

+20

i didn't knew that😅 Thanks for info

+17

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

+16

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

+14

Knew it😄 But it will be helpful for others.👍👍

+12

thanks for info📕 What does the Python say? This is helpfull !!!👍👍👍

+11

Great 😊i didn't knew that 😃

+10

Great . I didn't know that . Thanx

+9

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

+9

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.

+8

Thanks Matthias. Never thought of it in that way.

+8

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" .

+8

always had this thing in my mind but never bothered to find it out thanlks forr sharingg 🤗

+8

Yes! i found out when i was researching for my histogram contest. i used it to draw histogram in this code 😁👇👇 https://code.sololearn.com/cvODAhcEuh2t/?ref=app

+7

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

+7

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.

+7

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 '''