Python字符串
在Python中,万物皆对象,显然字符串是对象类型,用str表示。字符串类型通常用单引号或者双引号包裹起来。
>>> "Hello,world"
'Hello,world'
>>> 'Hello,world'
'Hello,world'
>>> type("Hello,world")
>>>
变量和字符串
在Python中变量无类型,对象有类型
>>> b = "hello,world"
>>> b
'hello,world'
>>> print b
hello,world
>>> type(b)
连接字符串
>>> "py" + "thon"
'python'
>>>
>>> a = 1989
>>> b = "free"
>>> print a + b
Traceback (most recent call last):
File "", line 1, in
print a + b
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> print b + `a` #反引号将整数a转化为字符串
free1989
>>>
用“+”拼接起来的字符串的两个对象必须是同一类型的。如果是数字则是求和,如果是字符串则得到一个新的字符串。
还有其他两种方法可以将整数转化为字符串:
str():将整数对象转换为字符串对象
>>> print b + str(a)
free1989
repr():相当于反引号``
>>> print b + repr(a)
free1989
int():将字符串对象转换为整数对象
>>> a = "250"
>>> b = int(a)
>>> b
250
>>> type(b)
>>>
转义字符
在字符串中,总会有一些特殊的符号,就需要用转义符。所谓转义,就是不采用符号本来的含义,而采用另外一含义。下面表格中列出常用的转义符:
转义字符
描述
\
(在行尾时) 续行符
\
反斜杠符号
'
单引号
"
双引号
a
响铃
b
退格(Backspace)
e
转义
000
空
n
换行
v
纵向制表符
t
横向制表符
r
回车
f
换页
oyy
八进制数,yy代表的字符,例如:o12代表换行
xyy
十六进制数,yy代表的字符,例如:x0a代表换行
other
其它的字符以普通格式输出
原始字符串
用转义符能够让字符串中的某些符号表示原来的含义,而不是被解析成某种具有特别能力的符号。下面看看这个代码:
>>> dos = "c:\news"
>>> dos
'c:\news'
>>> print dos #当用print打印时就出现问题了
c:
ews
如何避免上述代码的问题?有两种方法,其一是前面介绍的转义符解决。
>>> dos = "c:\\news"
>>> print dos
c:\news
>>>
其二是声明为原始字符串
>>> dos = r"c:\news"
>>> print dos
c:\news
>>> dos = r"c:\news\python"
>>> print dos
c:\news\python
如r"c:news",由r开头引起的字符串就是声明了后面引号里的东西是原始字符串,在里面放任何字符都表示该字符的原始含义,不需要再用转义符了。
raw_input和print
下面实现接收和打印用户通过键盘输入的内容:
不过在写这个功能前,要了解函数:
Python 2:raw_input()
Python 3: input()
这是Python的内建函数(built-in function)。关于内建函数,可以分别通过下面的链接查看:
关于Python的内建函数,下面列出来,供参考:
abs() divmod() input() open() staticmethod()
all() enumerate() int() ord() str()
any() eval() isinstance() pow() sum()
basestring() execfile() issubclass() print() super()
bin() file() iter() property() tuple()
bool() filter() len() range() type()
bytearray() float() list() raw_input() unichr()
callable() format() locals() reduce() unicode()
chr() frozenset() long() reload() vars()
classmethod() getattr() map() repr() xrange()
cmp() globals() max() reversed() zip()
compile() hasattr() memoryview() round() __import__()
complex() hash() min() set()
delattr() help() next() setattr()
dict() hex() object() slice()
dir() id() oct() sorted()
怎么才能知道哪个函数怎么用,并且用来干什么的呢?
>>> raw_input("input your name: ")
input your name: michael
'michael'
>>> name = raw_input("input your name: ")
input your name: michael
>>> name
'michael'
>>> type name
SyntaxError: invalid syntax
>>> type(name)
>>> age = raw_input("How old are you? ");
How old are you? 25 #输入数字
>>> age
'25'
>>> type(age)
#返回仍是str类型
>>> print "Hello, world"
Hello, world
>>> a = "python"
>>> print a
python
>>> b = "good"
>>> print a,b
python good
>>>
特别提醒的是,print的返回值默认是以n结尾的,所以每个输出语句之后自动换行。
索引和切片
索引
在Python中,把像字符串这样的对象类型统称为序列,顾名思义,序列就是有序排列。在这个序列中,每个人都有编号,编号和每个字符一一对应,在Python中这些编号称为索引。
>>> lang = "study python"
>>> lang[0]
's'
>>> lang[1]
't'
也可以这样做:
>>> "study python"[0]
's'
>>> "study python"[1]
't'
>>>
通过字符找到其在字符串中的索引值:
>>> lang = "study python"
>>> lang.index("p")
6<