python中的转义字符

来源:互联网 发布:微信摇塞子作弊软件 编辑:程序博客网 时间:2024/05/18 04:45

字符串是由‘’或者“”括起来的,如果所表示的字符串中含有引号,那怎么办呢?

1.如果字符串中包含“”,例如:

'learn "python" online '

字符串外面用‘’括起来。

2.如果字符串中既包含‘ 又包含“ ,例如:

 he said "I'm hungry."
'he said \" I\'m hungry .\"'

在 ’和“ 前面各插入一个转义字符\ 。
注:转义字符\不计入字符串的内容。
这里写图片描述

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 sm= 'Today is 2017\\3\\26.'print m

其中换行时\n后空格并不影响。

这里写图片描述

0 0
原创粉丝点击