字符串
- 写法:
a = 'hello'
b = "python"
c = """
用户管理系统
1.添加用户
2.删除用户
3.显示用户
"""
1. 字符串常用的转义符号
- \n:换行
- \t:一个tab键
- "
- ’
# 打印guido's
# 打印"hello guido's python"
print('guido\'s')
print("guido's")
print('"hello guido\'s python"')
print("\"hello guido's python\"")
print('%s\n%s' %(a,b))
print('%s\t%s' %(a,b))
2. 字符串特性
s = ‘hello’
- 索引:0,1,2,3,4 索引是从0开始的
print(s[0]) #h
print(s[4]) #o
- 拿出字符串的最后一个字符
print(s[-1])
2.1 切片
- s[start?step] 从start开始,到end-1结束,步长为step(默认是1)
print(s)
print(s[0:3]) #hel
print(s[0:4:2]) #h l
- 显示所有字符
print(s[:])
- 显示前3个字符
print(s[:3])
- 字符串倒序输出
print(s[::-1])
- 除了第一个字符之外,其他的全部显示
print(s[1:])
- 例子:
## 题目要求:
#判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
num = input('Num:')
print(num == num(::-1))
#不用字符串的形式,用整数来判断?
num = input('Num:')
n=num
new_num=0
while num:
i=num%10
num=num//10
new_num=new_num*10+i
if new_num==n:
print('是')
else:
print('否')
2.2 重复
print(s*10)
2.3 连接
print('hello '+‘world’)
2.4 成员操作符
print(‘he’ in s)
print(‘aa’ in s)
print(‘he’ not in s)
3. 匹配字符串的开头和结尾
filename='hello.logggh'
if filename.endswith('.log'):
print(filename)
else:
print('error file')
url1 = 'file:///mnt'
url2 = 'ftp://172.25.254.250/pub/'
url3 = 'http://172.25.254.250/index.html'
if url3.startswith('http://'):
print('爬取网页')
else:
print('不能爬取网页')
4. 字符串去掉两边的空格
In [1]: s = ' hello'
In [2]: s.strip()#去掉两边的
Out[2]: 'hello'
In [3]: s = ' hello '
In [4]: s.strip()
Out[4]: 'hello'
In [5]: s.lstrip()#去掉左边的
Out[5]: 'hello '
In [6]: s.rstrip()#去掉右边的
Out[6]: ' hello'
In [7]: s = '\nhello '
In [8]: s.strip()
Out[8]: 'hello'
In [9]: s = '\thello '
In [10]: s.strip()
Out[10]: 'hello'
In [11]: s = 'helloh'
In [12]: s.strip('h')#去掉h
Out[12]: 'ello'
In [13]: s.strip('he')
Out[13]: 'llo'
In [14]: s.lstrip('he')#去掉左边的he
Out[14]: 'lloh'
In [15]: s.rstrip('he')#去掉右边he
Out[15]: 'hello'
In [17]: print('学生管理系统'.center(50,'*'))#放在50个*的中间
**********************学生管理系统**********************
In [18]: print('学生管理系统'.ljust(50,'*'))#放在左边
学生管理系统********************************************
In [19]: print('学生管理系统'.rjust(50,'*'))#放在右边
********************************************学生管理系统
5. 搜索和替换
- find
- replace
- count
In [20]: s = 'hello python,learn python'
In [21]: s.find('python')#寻找python所在的位置
Out[21]: 6
In [22]: s.rfind('python')#右边的
Out[22]: 19
In [23]: s.replace('python','linux')#替换
Out[23]: 'hello linux,learn linux'
In [24]: s1 = s.replace('python','linux')
In [25]: s1
Out[25]: 'hello linux,learn linux'
In [26]: s
Out[26]: 'hello python,learn python'
In [27]: s.count('python')#查找有多少个python
Out[27]: 2
In [28]: s.count('p')
Out[28]: 2
In [29]: s.count('i')
Out[29]: 0
6. 字符的分离和拼接
- split
- join
In [30]: ip = '172.25.254.10'
In [31]: ip1 = '1172.25.254.10'
In [32]: ip1.split('.')#以.分离
Out[32]: ['1172', '25', '254', '10']
In [33]: date = '2018-11-18'
In [34]: date.split('-')
Out[34]: ['2018', '11', '18']
In [35]: date.split('.')
Out[35]: ['2018-11-18']
In [37]: date.replace('-','/')#替换
Out[37]: '2018/11/18'
In [38]: ip = ['1172', '25', '254', '10']
In [39]: ''.join(ip)#拼接
Out[39]: '11722525410'
In [40]: ':'.join(ip)#用:拼接
Out[40]: '1172:25:254:10'
In [41]: '*'.join(ip)
Out[41]: '1172*25*254*10'