第4章 基本概念
字面意义上的常量
如5、1.23、9.23e-3,或者‘This is a string‘、"It‘s a string!" 字符串等
数
python 有4种数据类型:整数、长整数、浮点数和复数。
字符串
单引号(‘)
如 ‘Quote me on this‘ 所有的空白,即空格和制表符都照原样保留。
双引号(")
双引号中字符串与单引号中的字符串的使用完全相同,如"What‘s your name?"
三引号(‘‘‘ 或 """)
使用三引号可以指示一个多行的字符串,可以在三引号中使用单引号和双引号。
‘‘‘This is a multi-line string. This is the firstline. This is the second line. "What‘s your name?," I asked. He said "Bond, James Bond." ‘‘‘ |
转义符(\)
用于特殊符号的转义。另外,在一个字符串中,行末的单独一个反斜杠表示字符串在下一行继续,而不是开始一个新的行。
"This is the first sentence.\ This is the second sentence." |
等价于 "This is the first sentence. This is the second sentence."
自然字符串
给字符串加上前缀 r 或 R 来保存字符串的原格式。例如 r"Newlines are indicated by\n"。
Unicode 字符串
Unicode 是书写国际文本的标准方法。在字符串前加上前缀 u 或 R,来处理 Unicode 文本。
处理含有非英语语言写的文本文件时使用 Unicode 字符串。如,print u‘你好‘ 。
字符串是不可变的
字符串一旦被创建就不能再改变了。
按字面意义级连字符串
两个字符串按字面意义相邻放着,会被 python 自动级连。
例如,‘What\‘s‘‘your name?‘ 会被自动转为 "What‘s your name?"。
原文:http://www.cnblogs.com/blueskylcc/p/5331002.html