python中的字符串
01.字符串的定义
a = "hello"
b = 'westos'
c = "what's up"
d = 'what\'s up'
e = """
用户管理系统
1.添加用户
2.删除用户
3.显示用户
"""
print(a)
print(b)
print(c)
print(d)
print(e)
print(type(e))
02.字符串的特性
01.索引: 0,1,2,3,4 索引值默认从0开始
s = ‘hello’
print(s[1])
print(s[0])
02.切片
print(s[0:3]) #切片的规则: s[start????step] 从start开始,到end-1结束,步长:step
print(s[0:4:2])
##显示所有字符
print(s[:])
#显示前3个字符
print(s[:3])
#字符串逆序输出
print(s[::-1])
#除了第一个字符以外,其他全部输出
print(s[1:])
03.重复
print( s * 10)
04.连接
print(‘hello’ + ’ ’ + ‘world’)
05.成员操作符
print(‘h’ in s)
print(‘q’ in s)
03.练习:回文数判断
num = input('Num:')
if num == num[::-1]:
print('是回文数')
else:
print('不是回文数')
04.字符串的判断
判断字符串里面每个元素是否为某种类型
print('123'.isdigit()) ##判断是否为数字
print('123abc'.isdigit())
print('123'.isalpha()) ##判断是否为字母
print('abc'.isalpha())
print('hello123'.isalnum()) ##判断是否为字母或数字
print('hello'.isalnum())
print('123'.isalnum())
print('hello.123'.isalnum())
title:首字母大写,其余字母小写
print('Hello'.istitle())
print('HeLlo'.istitle())
字母的大小写转换与判断
print('hello'.upper())
print('hello'.isupper())
print('hElLo'.lower())
print('hElLo'.islower())
05.字符串去掉开头与结尾
# In [8]: s = ' hello '
#
# In [9]: s.strip() ##.strip()默认去掉开头和结尾
# Out[9]: 'hello'
#
# In [10]:
#
# In [10]: s.lstrip() ##.lstrip()去掉左侧/开头
# Out[10]: 'hello '
#
# In [11]: s.rstrip() ##.lstrip()去掉右侧/结尾
# Out[11]: ' hello'
#
# In [12]: s = ' \nhello '
#
# In [13]: s.strip() ##.strip()默认去掉开头和结尾包括空格和空行
# Out[13]: 'hello'
#
# In [14]: s = ' \thello '
#
# In [15]: s.strip()
# Out[15]: 'hello'
#
# In [16]: s = 'helloh'
#
# In [17]: s.strip('h') ##从全部中去掉指定的字符
# Out[17]: 'ello'
#
# In [18]: s.lstrip('he') ##去掉左侧的he
# Out[18]: 'lloh'
06.字符串匹配开头和结尾
匹配结尾使用.endswith(’ ')
filename = 'hello.log'
if filename.endswith('.log'): ##匹配以.log结尾的
print(filename)
else:
print('error')
匹配开头使用.startswith(’ ')
filename = 'hello.log'
if filename.startswith('he'):
print(filename)
else:
print('error')
07.练习:判断变量名是否合法
“”"
变量名是否合法:
1.变量名只能由字母、数字、下划线组成
2.只能以字母或下划线开头
“”"
步骤:
#1.变量名第一个字符是否为字母或者下划线
#2.如果是,继续 --> 4
#3.如果不是,报错 , 退出
#4.依次判断除了第一个字符以外的其他字符
#5.判断是否为字母数字或者下划线
代码如下:
while True: ##死循环,可以一直判断直到退出
s = input('变量名(exit退出):')
if s == 'exit':
print('欢迎下次使用')
break
if s[0].isalpha() or s[0] == '_':
for i in s[1:]:
if not (i.isalnum() or i == '_'):
print('%s变量名不合法' %s)
break
else:
print('%s变量名合法' %s)
else:
print('%s变量名不合法' %s)
08.字符串的搜索和替换
s = 'hello world hello'
#find找到子串,并返回最小的索引
print(s.find('hello'))
print(s.find('world'))
#rfind找到子串,并返回最大的索引值
print(s.rfind('hello'))
#替换字符串中所有的‘hello’为‘westos’
print(s.replace('hello','westos'))
09.字符串的对齐
print('学生管理系统'.center(30)) ##.center()居中对齐,默认用空格补齐
print('学生管理系统'.center(30,'*')) ##使用*补齐
print('学生管理系统'.center(30,'@'))
print('学生管理系统'.ljust(30,'*')) ##.ljust()靠左对齐,用*补齐
print('学生管理系统'.rjust(30,'*')) ##.rjust()靠右对齐,用*补齐
10.字符串的统计
print('hello'.count('l')) ##统计l的个数
print('hello'.count('ll')) ##统计ll的个数
print(len('hello')) ##统计总的字符个数
11.字符串的分离和链接
s = '172.25.254.250'
s1 = s.split('.') ##以.为分割符分离并保存到s1中
print(s1)
print(s1[::-1]) ##可以调整为逆序
date = '2019-03-17'
date1 = date.split('-') ##以-为分隔符分离并保存到date1中
print(date1)
print(''.join(date1)) ##将date1中字符串直接链接起来i
print('/'.join(date1)) ##将date1中的字符串以-为分隔连接起来
练习:
1.要求:
“”"
输入
hello xiao mi
输出
mi xiao hello
“”"
实现:
print(' '.join(input().split()[::-1]))
2.要求:
“”"
输入
They are students.
aeiou
输出
Thy r stdnts.
“”"
实现:
s1 = input('s1:')
s2 = input('s2:')
for i in s1:
if i in s2:
s1 = s1.replace(i,'')
print(s1)