SoFunction
Updated on 2024-11-10

Usage notes for python and

1. () and input

import sys
# () Equivalent to input, the difference is that input doesn't read in '\n'.
aa = ()
bb = input('Please enter:') 
print(len(aa))
print(len(bb))
 
#Results
i love DL
Please enter:i love DL
10
9

So there is an extra '\n' in the len(aa) element. Another difference is that inside input() you can pass in text directly and print it out.

2. With print

('hello' + '\n')
print('hello')

The two lines above are equivalent, for example:

import sys
# () Equivalent to input, the difference is that input doesn't read in '\n'.
aa = ()
bb = input('Please enter:') 
(str(len(aa)) + '\n')
print(len(bb))
 
#Results
i love DL
Please enter:i love DL
10
9

Note: The obj in (obj+'\n') can only be a string.

Addendum: The role of () in Python

The way the buffer is flushed:

flush() flushes the cache.

Automatic refresh when buffer is full

File closure or program end auto refresh.

import time
import sys 
for i in range(5):
 print(i,end='')
 # ()
 (0.001)
#Annotations have different effects when they are turned on and off

When we print some characters, they are not printed immediately after calling the print function. Usually the characters are sent to the buffer first and then printed.

This presents a problem if you want to wait for an interval to print some characters, but it won't print because the buffer isn't full.

It would require some means. Such as forcibly refreshing the buffer after each print.

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more. If there is any mistake or something that has not been fully considered, please do not hesitate to give me advice.