print() formats the output, while write() just prints the characters it is given. print() handles many argument types, converting them into printable strings of characters with String.valueOf(), while write() just handles single characters, arrays of characters, and strings.
To illustrate the difference, write(int) interprets the argument as a single character to be printed, while print(int) converts the integer into a character string. write(49) prints a "1", while print(49) prints "49".
source: http://www.coderanch.com/t/398792/java/java/write-print
Answer from Maantje on Stack Overflowprint() formats the output, while write() just prints the characters it is given. print() handles many argument types, converting them into printable strings of characters with String.valueOf(), while write() just handles single characters, arrays of characters, and strings.
To illustrate the difference, write(int) interprets the argument as a single character to be printed, while print(int) converts the integer into a character string. write(49) prints a "1", while print(49) prints "49".
source: http://www.coderanch.com/t/398792/java/java/write-print
write(int) writes a single character (hence takes the Unicode character of the passed int).
print(datatype) on the other hand converts the datatype (int, char, etc) to a String by first calling String.valueOf(int) or String.valueOf(char) which is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.
For more details, you can refer the documentation of PrintWriter.
python - f.write vs print >> f - Stack Overflow
Difference between `write` and `print`
meaning - What's the difference between print and write? - English Language & Usage Stack Exchange
[deleted by user]
print does things file.write doesn't, allowing you to skip string formatting for some basic things.
It inserts spaces between arguments and appends the line terminator.
print "a", "b" # prints something like "a b\n"
It calls the __str__ or __repr__ special methods of an object to convert it to a string.
print 1 # prints something like "1\n"
You would have to manually do these things if you used file.write instead of print.
I disagree somewhat with several of the opinions expressed here, that print >> f is redundant and should be avoided in favour of f.write.
print and file.write are quite different operations. file.write just directly writes a string to a file. print is more like "render values to stdout as text". Naturally, the result of rendering a string as text is just the string, so print >> f, my_string and f.write(my_string) are nearly interchangeable (except for the addition of a newline). But your choice between file.write and print should normally be based on what you're doing; are you writing a string to a file, or are you rendering values to a file?
Sure, print is not strictly necessary, in that you can implement it with file.write. But then file.write is not strictly necessary, because you can implement it with the operations in os for dealing with file descriptors. Really they're operations on different levels, and you should use whichever is more most appropriate for your use (normally the level other nearby code is working on, or the highest level that doesn't get in your way).
I do feel that the print >> f syntax is fairly horrible, and is a really good example of why print should have been a function all along. This is much improved in Python 3. But even if you're writing Python 2 code that you're planning to port to Python 3, it is much easier to convert print >> f, thing1, thing2, thing3, ... to print(thing1, thing2, thing3, file=f) than it is to convert the circumlocution where you roll your own code to do the equivalent of print's rendering and then call f.write(text). I'm pretty sure the semi-automatic converter from Python 2 to Python 3 will even do the conversion for you, which it couldn't possibly do if you avoid the print >> f form.
Bottom line: use print to render values to stdout (or to a file). Use f.write to write text to a file.
I ran across sys.stdout.write("This appears to serve the same purpose as print.") in a bit of code. It requires importing sys, right? What does it offer over printing? Or in what case would you want to use it?
file_object = open(“testfile.txt”,”w”)
If I want to write on this file, I see people using the format:
file_object.write(“Hello World”)
And other people use the format
print("Hellow World", file=file_object)Which is more conventional? And is there any difference?
Thanks
Serial.write is more down to earth , it is simple and fast, it is made to talk binary, one byte at a time. example:
Serial.write(0x45); // will write 0100 0101 to the cable
Serial.print in the other hand is more versatile , it will do the conversion for you from ASCII to binary it also can convert to BIN/ HEX/OCT/DEC but you need to specify a second argument like so
Serial.print(76, BIN) gives "0100 1100"
Serial.print(76, OCT) gives "114"
Serial.print("L", DEC) gives "76"
Serial.print(76, HEX) gives "4C"
more examples with visual serial output :
Code:
Serial.write(0x48); // H
Serial.write(0x45); // E
Serial.write(0x4C); // L
Serial.write(0x4C); // L
Serial.write(0x4F); // O
SERIAL OUTPUT :

Code:
Serial.print("HELLO");
SERIAL OUTPUT :

Serial.println() in the other hand will add end of line 2 bytes 0x0D and 0x0A as you can see in the frame
Code:
Serial.println("HELLO");
SERIAL OUTPUT :

From the Arduino site for Serial.write and Serial.print:
Serial.write()
Writes binary data to the serial port. This data is sent as a byte or series of bytes; to send the characters representing the digits of a number use the print() function instead.
Serial.print()
Prints data to the serial port as human-readable ASCII text. This command can take many forms. Numbers are printed using an ASCII character for each digit. Floats are similarly printed as ASCII digits, defaulting to two decimal places. Bytes are sent as a single character. Characters and strings are sent as is