1.字符串的定义方式
代码:
# 字符串的定义
a = 'hello'
# \表示转义
b = 'what\'s up'
# 也可以直接使用双引号
c = "what's up"
print(a)
print(b)
print(c)
运行结果:
2.字符串的6大特性
索引 切片 重复 连接 成员操作符 for循环遍历(迭代)
(1).索引
代码:
s = 'hello'
# 元素的下标一般从0开始
print(s[0])
print(s[1])
运行结果:
(2).切片
代码:
s = 'hello'
# 切片的规则:
# s[start:end:step] 从start开始到end-1结束,步长:step
print(s[0:3])
print(s[1:2])
print(s[0:4:2])
# 显示所有字符
print(s[:])
# 显示前3个字符(0 1 2)
print(s[:3])
# 从第0个开始到最后一个的前一个结束
print(s[:-1])
# 对字符串倒序输出
print(s[::-1])
# 显示除了第一个字符以外的其他全部字符
print(s[1:])
运行结果:
(3).重复
代码:
s = 'hello'
# 将字符串s连续打印5次
print(s * 5)
运行结果:
(4).连接
代码:
s = 'hello'
print(s + ' word!')
运行结果:
(5).成员操作符
代码:
s = 'hello'
# 判断h是否在字符串s中
print('h' in s)
运行结果:
(6).for循环遍历 (迭代)
代码:
for i in s:
print(i)
运行结果:
示例1:回文数的判断
例如:121就是一个回文数
示例 1:
输入: 121
输出: true
示例 2:
输入: -121
输出: false
解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因>此它不是一个回文数。
示例 3:
输入: 10
输出: false
解释: 从右向左读, 为 01 。因此它不是一个回文数。
代码:
num = input('num:')
# num[::-1]表示字符串倒序输出
if num[::-1] == num:
print('这是一个回文数')
else:
print('这不是一个回文数')
运行结果:
3.字符串判断大小写和数字
判断字符串里每个元素是否为某种类型,一旦有一个元素不满足,就返回False
代码1:
# digit:数字;判断某个字符串是否为数字
print('123'.isdigit())
print('123abc'.isdigit())
运行结果:
代码2:
# title:标题;判断某个字符串是否为标题(第一个字母大写,其余字母小写)
print('Hello'.istitle())
print('HeLLo'.istitle())
运行结果:
代码3:
# upper:大写字母;将字符串转换为大写
print('hello'.upper())
# 判断字符串是否为大写
print('hello'.isupper())
运行结果:
代码4:
# lower:小写字母
print('HELLO'.lower())
print('HELLO'.islower() )
运行结果:
代码5:
# alpha:字母
print('123'.isalpha())
print('abc'.isalpha())
运行结果:
代码6:
# alnum:数字或字母
print('hello123'.isalnum())
运行结果:
4.字符串开头结尾的匹配
(1).匹配开头
代码:
# 定义字符串
url1 = 'file:///mnt'
url2 = 'ftp://172.25.254.250/pub'
url3 = 'https://172.25.254.250/index.html'
# startswith() 表示以...开头
if url3.startswith('http://'):
print('获取网页')
else:
print('未找到网页')
运行结果:
(2).匹配结尾
代码:
# 定义字符串
filename = 'hello.logg'
# endswith() 表示以...结尾
if filename.endswith('.log'):
print(filename)
else:
print('error filename')
运行结果:
5.字符串去除两边空格
(1).去掉空格
>>> s = ' hello '
>>> s
' hello '
# 去除两边的空格
>>> s.strip()
'hello'
#去除右边的空格(right)
>>> s.rstrip()
' hello'
#去除左边的空格(left)
>>> s.lstrip()
'hello '
(2).去掉指定字符
>>> s = 'helloh'
# 去掉两边的h
>>> s.strip('h')
'ello'
# 去掉右边的h
>>> s.rstrip('h')
'hello'
# 去掉左边的h
>>> s.lstrip('h')
'elloh'
代码:
# \n表示换行, \t表示横向制表符(8个空格)
s = '\nhello\t\t'
print(s)
# 去掉两边的空格(换行符也表示空格)
print(s.strip())
运行结果:
6.字符串的搜索与替换
(1)find 搜索
代码:
s = 'hello word hello'
# 注意:下标从0开始,空格也算一个字符
# find 表示找到子串,并返回最小的索引
print(s.find('hello'))
print(s.find('word'))
# rfind 表示找到子串,并返回最大索引
print(s.rfind('hello'))
运行结果:
(2)replace 替换
代码:
s = 'hello word hello'
# replace 表示将字符串中所有的'hello'替换为'westos'
print(s.replace('hello','westos'))
运行结果:
7.字符串的对齐
# center 表示居中对齐,默认用空格补齐剩余元素
print('学生管理系统'.center(30))
# 居中对齐,选用*号补齐剩余元素
print('学生管理系统'.center(30,'*'))
# ljust 表示左对齐
print('学生管理系统'.ljust(30,'*'))
# rjust 表示右对齐
print('学生管理系统'.rjust(30,'*'))
8.字符串的分离和连接
(1).分离
代码:
s = '172.25.254.66'
# split:分离; 通过指定的字符进行分离
s1 = s.split('.')
print(s1)
# 倒序输出字符串
print(s1[::-1])
运行结果:
(2).连接
代码:
date = '2019-01-15'
# 以-为分隔符进行分离
date1 = date.split('-')
print(date1)
# join:连接; 通过指定的字符进行连接
print(''.join(date1))
# 通过#进行连接
print(':'.join(date1))
运行结果:
9.如何快速生成验证码,内推码
(1)生成字母和数字
import string
# 1.打印所有的字母:a-z , A-Z
print(string.ascii_letters)
# 2.打印所有的数字:0-9
print(string.digits)
# 3.打印所有的数字和字母, + 表示连接
code_str = string.ascii_letters + string.digits
print(code_str)
(2)生成验证码 / 内推码
方法1:
import random
import string
# 生成所有的数字和字母
code_str = string.ascii_letters + string.digits
# 定义函数:生成验证码
def gen_code(len):
# random.sample:随机生成括号内的指定长度的字符
return ''.join(random.sample(code_str,len))
print(list(gen_code(4)))
方法2:
import random
import string
code_str = string.ascii_letters + string.digits
# 定义函数:生成验证码
def gen_code():
# random.choice(): 随机生成括号内的字符
# join: 按照指定字符连接
return ''.join(random.choice(code_str))
print([gen_code() for i in range(4)])