字符串的定义
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键
\t:在控制台输出一个制表符,协助我们在输出文本时在垂直方向保持对齐
\"
\'
例如:
打印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))
字符串的特性
s = 'hello'
索引:0,1,2,3,4 索引是从0开始的
print(s[0])
print(s[4])
拿出字符串的最后一个字符
print(s[-1])
切片
s[start : end : step] 从start开始,到end-1结束,步长为step(默认是1)
print(s)
print(s[0:3])
print(s[0:4:2])
显示所有字符
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)
字符串特性的应用
例如:
要求:判断一个整数是否是回文数。(回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。)
示例:
示例 1:
输入: 121
输出: true
示例 2:
输入: -121
输出: false
解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
示例 3:
输入: 10
输出: false
解释: 从右向左读, 为 01 。因此它不是一个回文数。
num = input('Num:')
if (num == num[::-1]):
print('%s是一个回文数' %(num))
else:
print('%s不是一个回文数' %(num))
字符串开头和结尾匹配
例如:
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('不能爬取网页')
字符串去掉两边的空格
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 [16]: print('学生管理系统'.center(50,'*'))
**********************学生管理系统**********************
In [17]: print('学生管理系统'.ljust(50,'*'))
学生管理系统********************************************
In [18]: print('学生管理系统'.rjust(50,'*'))
********************************************学生管理系统
字符串的搜索,替换和计数
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
字符串的分离和拼接
split:分离
join:拼接
In [31]: ip1 = '172.25.254.10'
In [32]: ip1.split('.')
Out[32]: ['172', '25', '254', '10']
In [33]: date = '2018-11-18'
In [34]: date.split('-')
Out[34]: ['2018', '11', '18']
In [35]: date.replace('-','/')
Out[35]: '2018/11/18'
In [36]: ip = ['172', '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'
判断字符串变成‘标题’格式
In [1]: 'Hello'.istitle()
Out[1]: True
In [2]: 'hello'.istitle()
Out[2]: False
In [4]: 'heLLo'.islower()
Out[4]: False
In [5]: 'heLLo'.isupper()
Out[5]: False
将字符串全部变为大写
In [6]: 'hello'.upper()
Out[6]: 'HELLO'
将字符串全部变为小写
In [7]: 'heLLo'.lower()
Out[7]: 'hello'
将字符串变成标题格式
In [8]: 'heLLo'.title()
Out[8]: 'Hello'
将字符串大小写互换
In [9]: 'heLLo'.swapcase()
Out[9]: 'HEllO'