In [15]: q = “qqq”
In [16]: w = ‘www’
In [17]: e = ”’ ….: eee ….: ”’
In [18]: type(q) Out[18]: str
In [19]: type(w) Out[19]: str
In [20]: type(e) Out[20]: str 转义 \t #代表tab键 \n #代表换行 \’ #代表单引号 \” #代表双引号
字符串的特性 索引 In [21]: a = ‘hello’
In [22]: a[0] Out[22]: ‘h’
In [23]: a[-1] Out[23]: ‘o’ 切片 In [24]: b = ‘hello word’
In [25]: b Out[25]: ‘hello word’
In [26]: b[:] Out[26]: ‘hello word’
In [27]: b[0:6] Out[27]: ‘hello ‘
In [28]: b[0:6:1] Out[28]: ‘hello ’ In [91]: b[0:6:2] Out[91]: ‘hlo’ 成员操作符 In [29]: a Out[29]: ‘hello’
In [30]: ‘o’ in a Out[30]: True
In [31]: ‘o’ not in a Out[31]: False 连接操作符 In [35]: a + b Out[35]: ‘hellohello word’ 重复操作符 In [32]: “*”*10 Out[32]: ‘****’
In [33]: i = “*”*10
In [34]: print i + “哈哈” + i **哈哈**
In [36]: print “哈哈”.center(30, “*”) **哈哈**
In [11]: a.ljust(30, ‘*’) Out[11]: ‘hello*********************’
In [12]: a.rjust(30, ‘*’) Out[12]: ‘*********************hello’ 结尾字符 In [1]: s = ‘hello’
In [2]: s Out[2]: ‘hello’
In [3]: s.endswith(‘o’) Out[3]: True
In [4]: s.endswith(‘o’,0,4) Out[4]: False
In [5]: s.endswith(‘e’,0,4) Out[5]: False
In [6]: s.endswith(‘l’,0,4) Out[6]: True
开头字符 In [7]: s.startswith(‘h’) Out[7]: True
In [8]: s.startswith(‘he’) Out[8]: True 查找 In [9]: s = ‘hellow word’
In [10]: s Out[10]: ‘hellow word’
In [11]: s.find(‘o’) Out[11]: 4
In [12]: s.find(‘o’,5) Out[12]: 8 替换 In [13]: s.replace(‘o’,’e’) Out[13]: ‘hellew werd’
In [14]: s.replace(‘o’,’e’,1) Out[14]: ‘hellew word’ 删除 In [15]: s = ’ hello ‘
In [16]: s.strip() Out[16]: ‘hello’
In [17]: s.lstrip() Out[17]: ‘hello ‘
In [18]: s.rstrip() Out[18]: ’ hello’
In [9]: s = “h e l l o”
In [10]: s.replace(” “, “”) Out[10]: ‘hello’ 一种应用 In [5]: input = raw_input(“input:”)#输入” 1” input: 1
In [6]: ‘1’ == input #判断是否等于”1” Out[6]: False
In [7]: input = raw_input(“input:”).strip() #去掉输入进去字符串的空格 input: 1
In [8]: ‘1’ == input #再次判断 Out[8]: True In [1]: info = “wes fa w2 f”
In [2]: info.spli info.split info.splitlines
In [2]: info.split() #将字符串分开后以列表的方式输出 Out[2]: [‘wes’, ‘fa’, ‘w2’, ‘f’] 比较 In [3]: cmp(“h”,”w”) Out[3]: -1
In [4]: cmp(“h”,”a”) Out[4]: 1
In [5]: cmp(“3”,”2”) Out[5]: 1
In [6]: max(‘1234’) Out[6]: ‘4’
In [7]: min(‘1234’) Out[7]: ‘1’ 枚举 In [1]: s = “hello”
In [2]: for i,j in enumerate(s): #将字符串以及它的索引值同时输出 …: print i,j …: 0 h 1 e 2 l 3 l 4 o zip In [3]: q = “qwer”
In [4]: w = “QWER”
In [5]: e = “123”
In [6]: zip(q,w,e) Out[6]: [(‘q’, ‘Q’, ‘1’), (‘w’, ‘W’, ‘2’), (‘e’, ‘E’, ‘3’)] 显示小数 In [1]: print “you money: %f” %(100000.88888888) you money: 100000.888889
In [2]: print “you money: %.2f” %(100000.88888888) you money: 100000.89
字符连接 In [8]: ” “.join(“hello”) Out[8]: ‘h e l l o’
In [9]: ” “.join([“ni”, “hao”]) Out[9]: ‘ni hao’
In [10]: “\n”.join([“ni”, “hao”]) Out[10]: ‘ni\nhao’
In [11]: print “\n”.join([“ni”, “hao”]) ni hao L.count() 查看某个字符出现了几次
L.insert(0.’ftp’) 往索引处加
L.reverse() 反转
L.sort() 排序,默认从小到大