Home  >  Article  >  Backend Development  >  Introduction to newlines and tabs in Python strings

Introduction to newlines and tabs in Python strings

不言
不言Original
2018-05-03 11:58:564049browse

The following is an introduction to newline characters and tab characters in Python strings. It has good reference value and I hope it will be helpful to everyone. Let’s take a look together

Questions about line breaks

First ask a question, as follows.

The python program code is as follows:

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

The output of the previous line of code is as follows:

I'm Bob. What's your name?

The above output has no line breaks. I want to wrap the line before What, but the effect is:

I'm Bob. 
What's your name?

What should I do?

Hit Enter before What, okay? No, the effect of this carriage return is to wrap the statement, not the output content.

Solution using newline characters

The solution to the above problem is to insert a newline character before What. The writing method is:

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

Tab character

The tab character also belongs to "The writing method is a combination of two characters , but the meaning is only one character" situation. It is written as "\t", which is a combination of a backslash and the letter t, where t means table. Its meaning is a character called tab. Its function is to align the columns of table data. Run the following code and you should understand what a tab character is.

#制表符的写法是\t,作用是对齐表格的各列。
print("学号\t姓名\t语文\t数学\t英语")
print("2017001\t曹操\t99\t\t88\t\t0")
print("2017002\t周瑜\t92\t\t45\t\t93")
print("2017008\t黄盖\t77\t\t82\t\t100")

Related recommendations:

How to traverse DataFrame in Python by row_python

The above is the detailed content of Introduction to newlines and tabs in Python strings. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn