1. 一些以前不知道的
1. //
9//3
:输出是3,//
表示取整
2. 三引号’’’
s = '''
This is the first line
This is the second line
'''
print(s)
结果为
3. +
- 两边是数字,相加
- 两边是字符串,连接
a = str(98.6)
a + "2"
结果为'98.62'
4. Shift+回车
- 一直向下运行代码
5. slice
letters = "abcdefghijklmnopqrstuvwxyz"
letters[0]
输出为'a'
letters = "abcdefghijklmnopqrstuvwxyz"
letters[0]
输出为‘a’
若为letters[-2]
,输出为‘安’
若为letters[:]
,输出为'我爱北京天安门'
若为letters[2:]
,输出为'北京天安门'
若为letters[-3:]
,输出为'天安门'
若为letters[:5]
,输出为'我爱北京天'
若为letters[:100]
,输出为'我爱北京天安门'
若为letters[2:5]
,输出为'北京天'
若为letters[1:2:5]
,输出为'爱'
若为letters[-6:-2]
,输出为'爱北京天'
6.split和combine
- 默认空格切分
lan = "python ruby c c++ swift"
lan.split()
输出['python', 'ruby', 'c', 'c++', 'swift']
- 按空格切
todos = "download python, install, download ide, learn"
todos.split(', ')
输出为:['download python', 'install', 'download ide', 'learn']
- join
','.join(['download python', 'install', 'download ide', 'learn'])
输出:'download python,install,download ide,learn'
7. replace
s = 'I like C. I like C++. I like Python'
s.replace('like', 'hate')
输出:'I hate C. I hate C++. I hate Python'
s.replace('like', 'hate', 1)
输出:'I hate C. I like C++. I like Python'
8. find
- 找下标