字符串:
- 字符串定义方式:
a = 'hello'
b = 'what\'s up'
c = "what's up"
d = """
用户管理系统
-查询
-添加
-删除
"""
print(a)
print(b)
print(c)
print(d)
输出结果:
hello
what's up
what's up
用户管理系统
-查询
-添加
-删除
- 字符串的常用转义字符:
\n 换行
\t 一个tab键 - 字符串的特性:索引,切片,重复,连接,成员操作符,迭代
- 索引
s = 'hello'
print(s[0])
print(s[1])
print(s[-1])
输出结果:
h
e
o
- 切片
s = 'hello'
print(s[0:3]) # 切片的规则:s[start:end:step] 从start开始到end-1结束,步长:step
print(s[0:4:2])
#显示所有字符
print(s[:])
#显示前3个字符
print(s[:3])
#对字符串倒叙输出
print(s[::-1])
#除了第一个字符以外,其他全部显示
print(s[1:])
输出结果:
hel
hl
hello
hel
olleh
ello
- 重复
s = 'hello'
print(s * 5)
输出结果:
hellohellohellohellohello
- 连接
s = 'hello'
print(s + 'world')
输出结果:
helloworld
- 成员操作符
s = 'hello'
print('h' in s)
输出结果:
True
- for循环(迭代)
s = 'hello'
for i in s:
print(i)
输出结果:
h
e
l
l
o
- 字符串匹配开头和结尾
- 匹配开头:
url1 = 'file:///mnt'
url2 = 'ftp://172.25.254.250/pub'
url3 = 'http://172.25.254.250/index.html'
if url3.startswith('https://'):
print('获取网页')
else:
print('未找到网页')
输出结果:
未找到网页
- 匹配结尾:
filename = 'hello.loggg'
if filename.endswith('.log'):
print(filename)
else:
print('error filename')
输出结果:
error filename
- 字符串去除两边空格
s.strip() 去掉两边空格
s.rstrip() 去除右边空格
s.lstrip() 去除左边空格
.strip(‘h’)去掉两边的h
.lstrip(‘h’)去掉左边的he
.rstrip(‘h’)去掉右边的he
注意: strip对\n,和\t都生效
In [1]: s = ' hello '
In [2]: s
Out[2]: ' hello '
In [3]: s.strip()
Out[3]: 'hello'
In [4]: s.rstrip()
Out[4]: ' hello'
In [5]: s.lstrip()
Out[5]: 'hello '
In [6]: s = '\nhello\t\t'
Out[7]: 'hello'
In [8]: s = 'helloh'
In [9]: s.strip('h') ##去除两边的h
Out[9]: 'ello'
In [10]: s.rstrip('h') ##去除右边的h
Out[10]: 'hello'
In [11]: s.lstrip('he') #去除左边的h
Out[11]: 'lloh'
- 字符串的对齐
.center(,) 居中
.ljust(,) 左对齐
.rjust(,) 右对齐
In [1]: print('学生管理系统'.center(20))
学生管理系统
In [2]: print('学生管理系统'.center(20,'*')) #居住,其余以*补齐,共20个字符
*******学生管理系统*******
In [3]: print('学生管理系统'.ljust(20,'*'))
学生管理系统**************
In [4]: print('学生管理系统'.rjust(20,'*'))
**************学生管理系统
- 字符串判断大小写和数字
- 判断字符串里每个元素是否为 什么类型
一旦有一个元素不满足,就返回False
print('123'.isdigit())
print('123abc'.isdigit())
输出结果:
True
False
- title:判断某个字符串是否为标题(第一个字母大写,其余字母小写)
print('Hello'.istitle())
print('HeLlo'.istitle())
输出结果:
True
False
- 转换成大写,并判断是否大写
print('hello'.upper())
print('hello'.isupper())
输出结果:
HELLO
False
- 转换成小写,并判断是否为小写
print('HELLO'.lower())
print('HELLO'.islower())
输出结果:
hello
False
- 判断是否为字母数字
print('hello123'.isalnum()) ##字母和数字(alnum)
print('123'.isalpha()) ##字母(alpha)
print('aaa'.isalpha())
输出结果:
True
False
True
- 练习:判断变量名是否合法:
“”"
题目要求:
1.变量名可以由字母,数字或者下划线组成
2.变量名只能以字母或者下划线开头
s = ‘hello@’
思路:
1.判断变量名的第一个元素是否为字母或者下划线 s[0]
2.如果第一个元素符合条件,判断除了第一个元素之外的其他元素s[1:]
“”" - 实现步骤:
#1.变量名的第一个字符是否为字母或下划线
#2.如果是,继续判断 --> 4
#3.如果不是,报错
#4.依次判断除了第一个字符之外的其他字符
#5.判断是否为字母数字或者下划线 - 代码:
while True:
s = input('变量名:')
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)
- 字符串的搜索、替换、统计
- find:搜索
- replace:替换
- count:统计
- len:统计长度
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'))
print('hello'.count('l'))
print('hello'.count('ll'))
print(len('hello'))
输出结果如下:
0
6
12
westos world westos
2
1
5
- 字符串的连接和分离
- split:分离
- join:拼接
s = '172.25.254.250'
s1 = s.split('.')
print(s1)
print(s1[::-1]) ##倒叙输出
date = '2019-01-15'
date1 = date.split('-')
print(date1)
#通过指定的字符进行连接
print(''.join(date1))
print('/'.join(date1))
输出结果:
['172', '25', '254', '250']
['250', '254', '25', '172']
['2019', '01', '15']
20190115
2019/01/15
练习:
1.回文数的判断
“”"
示例 1:
输入: 121
输出: true
示例 2:
输入: -121
输出: false
解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因>此它不是一个回文数。
示例 3:
输入: 10
输出: false
解释: 从右向左读, 为 01 。因此它不是一个回文数。
“”"
num = input(‘Num:’)
if num == num[::-1]:
print(‘这是一个回文数’)
else:
print(‘这不是一个回文数’)