Python: How to Set Line breaks and tabs for Strings

First of all, I would like to raise a question as follows.

With Python program code:

   print("I'm Bob. What's your name?") 

The output of the previous line is as follows:

      I’m Bob. What’s your name?

The output above does not wrap. If you want to wrap before what, the effect is:

     I’m Bob.
What’s your name?

What should we do?

Knock back before what, OK? No, the effect of this carriage return is to wrap the statement, not the output.

The solution of using newline character

The solution to this problem is to insert a newline before what. It is written as follows:

   print("I'm Bob.\nWhat's your name?")

Have you noticed the word in front of what? It’s a character combination, a combination of backslashes and n letters. However, the meaning of this combination is only one character, that is, the newline character.

Again, it’s a combination of two characters in writing, but only one character in meaning.

In Python language, in addition to the newline character, there are many cases where “the writing method is a combination of two characters, but the meaning is only one character”, and the tab character is one of them.


Tab also belongs to the situation that “the writing method is a combination of two characters, but the meaning is only one character”. It’s written as “ T”, a combination of backslash and T, and t means table. It means a character, called a tab. Its function is to align the columns of the table data. Run the following code, you should understand what tab is.

#The table-making character is written as \t and serves to align the columns of the table.

Running the above code produces the following output:

Student number: Chinese, mathematics, English
2017001 Cao Cao 99 88 0
2017002 Zhou Yu 92 45 93
2017008 Huang Gai 77 82 100

note that the writing of line breaks and tabs only works within quotation marks and is considered a character.

Read More: