1. 字符串的定义
a = 'hello'
b = "python"
c = """
用户管理系统
1.添加用户
2.删除用户
3.显示用户
"""
print(type(a))
print(type(b))
print(type(c))
print(a)
print(b)
print(c)
-
字符串常用的转义符号
\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]) ##输出l
-
拿出字符串的最后一个字符
print(s[-1]) ##输出o
-
切片
#s[start:end:step] 从start开始,到end-1结束,步长为step(默认是1) print(s) print(s[0:3]) ##输出hel print(s[0:4:2]) ##输出hl
-
#显示所有字符
print(s[:])
-
#显示前3个字符
print(s[:3])
-
#字符串倒序输出
print(s[::-1])
-
#除了第一个字符之外,其他的全部显示
print(s[1:])
-
重复
print(s*10) ##重复十遍
-
连接
print('hello '+'world')
-
成员操作符
print('he' in s) print('aa' in s) print('he' not in s)
例题:
判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样
的整数。例:12321
num = input('Num:')
print(num == num(::-1))
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')
Out[12]: 'ello'
In [13]: s.strip('he')
Out[13]: 'llo'
In [14]: s.lstrip('he')
Out[14]: 'lloh'
In [15]: s.rstrip('he')
Out[15]: 'hello'
In [17]: print('学生管理系统'.center(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')
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')
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'
7.字符串常用方法
- 判断字符串 变成‘标题’
In [1]: 'Hello'.istitle()
Out[1]: True
In [2]: 'hello'.istitle()
Out[2]: False
In [7]: 'heLLo'.islower()
Out[7]: False
In [8]: 'heLLo'.isupper()
Out[8]: False
- 将字符串全部变为大写
In [3]: 'hello'.upper()
Out[3]: 'HELLO'
- 将字符串全部变为小写
In [4]: 'heLLo'.lower()
Out[4]: 'hello'
In [5]: 'heLLo'.title()
Out[5]: 'Hello'
In [6]: 'heLLo'.swapcase()
Out[6]: 'HEllO'