版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
文章目录
1.字符串的定义
"""
a = 'westos'
b = "what's"
c = """
用户管理系统
1.添加用户
2.删除用户
3.显示用户
....
"""
print(c)
2.字符串的特性①(索引、链接等)
s = 'hello' # 索引:0 1 2 3 4 索引从0开始
print(s[0])
print(s[1])
# 拿出最后 一个字符
print(s[4])
print(s[-1])
# s[start:stop:step] 从satrt开始到end -1结束
# 步长为step
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 ' + 'python')
# 成员操作符
print('he' in s)
print('aa' in s)
print('he' not in s)
3.字符串判断大小写
'hello'.istitle()
False
'hello'.isupper()
False
'Hello'.isupper()
False
'HELLO'.isupper()
True
'hello'.islower()
True
'Hello'.islower()
False
4.判断字符串的结尾和开头
filename = 'hello.log'
if filename.endswith('.log'):
print(filename)
else:
print('error.file')
url = 'https://172.25.254.250/index.html'
if url.startswith('http://'):
print('爬取内容~~')
else:
print('不能爬取~~')
5.字符串的特性②
注意:去除左右两边的空格,空格为广义的空格 包括:\t \n
s = ' hello'
s
'\thello'
s = ' hello'
s
' hello'
s.lstrip()
'hello'
s = ' hello '
s
' hello '
s.lstrip()
'hello '
s.rstrip()
' hello'
s.strip()
'hello'
s = ' hello'
s
'\thello'
s.strip()
'hello'
s = ' hello\n'
s
' hello\n'
s.strip()
'hello'
s = 'helloh'
s.strip('h')
'ello'
s.lstrip('h')
'elloh'
6.字符串的判断
# 只要有一个元素不满足,就返回False
print('weeeqwe32131'.isdigit())
print('42131321fsas'.isalpha())
print('weewqeq323213'.isalnum())
7.字符串的对齐
print('学生管理系统'.center(30))
print('学生管理系统'.center(30,'@'))
print('学生管理系统'.center(30,'&'))
print('学生管理系统'.ljust(30,'#'))
print('学生管理系统'.rjust(30,'#'))
8.字符串的替换
s = 'hello world hello'
# find 找到子字符串,并返回最小的索引
print(s.find('hello'))
print(s.find('world'))
print(s.rfind('hello'))
# 替换字符串中的hello为redhat
print(s.replace('hello','redhat'))
9.字符串的分离和连接
s = '172.25.254.250'
s1 = s.split('.')
print(s1)
print(s1[::-1])
date = '2019-8-28'
date1 = date.split('-')
print(date1)
# 连接 通过指定的连接符号 连接每个字符
print(''.join(date1))
print('/'.join(date1))
print('~~'.join('hello'))