python笔记(2)
字符串
- 字符串是 Python 中最常用的数据类型。我们可以使用引号('或")来创建字符串。
- 它是一个线性结构
var1 = 'Hello World!'
var2 = "Python Runoob
print "var1[0]: ", var1[0]
print "var2[1:5]: ", var2[1:5]
结果为:
var1[0]: H
var2[1:5]: ytho
c='abcdefg'
print(c[2:5])#结果为cde
print(c[4:8])#efg
print(c[::-1])#gfed
字符串[开始值:结束值:步长值]
值
- Python 不支持单字符类型,单字符在 Python 中也是作为一个字符串使用。
- Python 访问子字符串,可以使用方括号来截取字符串
Python 转义字符
(在行尾时) 续行符
\\ 反斜杠符号
\’ 单引号
\" 双引号
\a 响铃
\b 退格(Backspace)
\e 转义
\000 空
\n 换行
\v 纵向制表符
\t 横向制表符
\r 回车
\f 换页
\oyy 八进制数,y 代表 0~7 的字符,例如:\012代表换行。
\xyy 十六进制数,以 \x 开头,yy代表的字符,例如:\x0a代表换行
\other 其它的字符以普通格式输出
运算符
+字符串连接
*重复输出字符串
[] 通过索引获取字符串中字符
[ : ] 截取字符串中的一部分
a='hello'
b='python'
print(a+b)#'hellopython'
print(a*2)#‘hellohello’
print(a[1])#'e'
print(a[1:4])#'ell'