1. Python中的单引号、双引号
Python中的单引号和双引号在单独使用时作用一样,都可以用来表达字符串,就相当于拼音输入法和五笔输入法都能打出汉字,但输入的内容不同而已。
我想要输出字符串:hello world
print('hello world')
print("hello world")
这两句代码都可以,输出的结果一样,都是:hello world
但是如果想要输出的字符串中含有单引号或双引号,就需要另一种符号的帮助了。举个栗子:
我想要输出字符串:She said, "hello world."
print('She said, "hello world."')
我想要输出字符串:Let's hello world.
print("Let's hello world")
可以看出,如果想要输出的字符串中已经出现一种引号,就用另一种成对出现的引号在最外层表示字符串。
2. 转义符
转义符的作用是告诉计算机,此处引号是字符串内容,不需要识别。举个栗子:
我想输出字符串:She said, "hello world." Let's hello world.
print('She said, \"hello word.\" Let\'s hello world.')
print("She said, \"hello word.\" Let\'s hello world.")
这两句代码都可以,最外层用单引号或双引号都可以,只要把字符串中的引号都用转义符标清楚就ok
3. 换行
换行可使用\n
我想要输出两行字符串:
She said, "hello world."
Let's hello world.
print("She said, \"hello word.\"\nLet\'s hello world.")