background

One of our new employees had a problem today, and the reason was surprisingly simple. Here to share with you.

Code:

print('first line'.'\n'.'second line')
Copy the code

The execution result

Analysis of the

Prints two characters at once

print('str1'.'str2')
Copy the code

Come to the conclusion:

Print two characters together, with a space between them by default. (I’m sure you know)

Analyze the problem code

Print (‘first line’,’\n’,’second line’), in which three characters are actually printed, each with a space between them, for a total of two Spaces.

The first space follows the first line, and the second space precedes the second line.

Oh, I see.

To solve the problem

Simply concatenate the last two characters with +. That is, the newline and second line are one string, so there is no space after the newline.

print('first line'.'\n'+'second line')
Copy the code

That’s all for today, thanks for reading, see you next time.