字符串是由‘’或者“”括起来的,如果所表示的字符串中含有引号,那怎么办呢?
1.如果字符串中包含“”,例如:
'learn "python" online '
- 1
字符串外面用‘’括起来。
2.如果字符串中既包含‘ 又包含“ ,例如:
he said "I'm hungry."
- 1
- 2
'he said \" I\'m hungry .\"'
- 1
在 ’和“ 前面各插入一个转义字符\ 。
注:转义字符\不计入字符串的内容。
3.常用的转义字符:
\n 表示换行
\t 表示一个制表符
\ 表示 \ 字符本身
任务
请将下面两行内容用Python的字符串表示并打印出来:
Python was started in 1989 by “Guido”.
Python is free and easy to learn.
Today is 2017\3\26.
s = 'Python was started in 1989 by \"Guido\".\n Python is free and easy to learn.'
print s
m= 'Today is 2017\\3\\26.'
print m
- 1
- 2
- 3
- 4
- 5
- 6
其中换行时\n后空格并不影响。