字符串
1.什么是字符串
- 用引号引起来的一串字符
- 用引号来创建字符串。
- 单引号
- 双引号
- 三单引号
- 三双引号
name = "HangZhou"
area = "GongShu"
history = "1500"
famous_person = """苏轼,法海"""
capticalof = '''吴越,南宋'''
print(type(name),type(area),type(history),type(famous_person),type(capticalof))
sent = "I'm Lilei"
sent2 = 'He is my brother,he said'
print(sent)
print(sent2)
#单引号和双引号使用时需要注意匹配关系,且不能换行
#若需要换行,使用三单引号或者三双引号
ex = '''这是第一行
这是第二行
换行记得三单三双'''
2.字符串的运算及常见操作
2.1拼接
拼接基于同一种数据类型
a = "hello"
b = ", python"
print(a+b)
a = ["类型相同"]
b = [",才能拼接"]
print(a+b)
2.2重复
a = "hello"
print(a * 3)
2.3索引(偏移) [] , 切片[:],[::]
sr = "Python"
for i in sr:
print(i,end=" ")
sr = "Python"
print(len(sr))
a = len(sr)
for i in range(a):
print(sr[i],end=" ")
#012345678901234567890123456789
sr = "life is short, you need python."
print(len(sr))
print(sr[0:5]) #life
#取01234 5之前
print(sr[1:4:2]) #ie 步长2 取13
print(sr[1::2]) #iei hr,yune yhn 135791357913579
2.4大小写转换
函数 | 功能 |
---|---|
.lower() | 转小写 |
.upper() | 转大写 |
.swapcase() | 大小写转换 |
.title() | 转为标题的形式 |
.capitalize() | 首字母大写 |
sr = "Life Is Short, You Need Python."
print(sr.upper())
print(sr.lower())
print(sr.swapcase())
print(sr.title())
print(sr.capitalize())
#LIFE IS SHORT, YOU NEED PYTHON.
#life is short, you need python.
#lIFE iS sHORT, yOU nEED pYTHON.
#Life Is Short, You Need Python.
#Life is short, you need python.
demo: 验证码确认
#验证码确认
sr = input("输入你看到的验证码,不区分大小写:")
srr = "LIFE"
if sr.upper() == srr:
print("输入正确")
else:
print("错误")
三次验证机会该如何做
2.5字符串的格式输出对齐
.center([len],[填充符号]) ,左对齐
sr = "Life Is Short, You Need Python."
print(sr.center(41,'#'))
print(sr.ljust(41,'$'))
print(sr.rjust(41,'^'))
print(sr.zfill(41)) 居右对齐,默认填充0
#####Life Is Short, You Need Python.#####
Life Is Short, You Need Python.$$$$$$$$$$
^^^^^^^^^^Life Is Short, You Need Python.
0000000000Life Is Short, You Need Python.
2.6删除指定字符
.strip() 删除开头结尾指定符号 若不给参数则默认删除\n\t等符号
.lstrip()
.rstrip()
sr = "#####Life Is Short, You Need Python.#####"
print(sr.strip('#'))
Life Is Short, You Need Python.
sr = "\n#####Life Is Short, You Need Python.#####\n"
print(sr.strip())
#####Life Is Short, You Need Python.#####
2.7计数
.count()
012345678901234567890123456789
sr = "Life Is Short, You Need Python."
print(sr.count('o',9,17))
#sr.count('统计元素',[start],[end]) 统计该元素在start,end-1出现的次数
2.8字符串搜索定位和替换
.find(),查找元素并返回第一次出现的元素索引值,若不存 返回 -1
.index()查找元素并返回第一次出现的元素索引值,若不存 报错
.rindex()从右往左查找
.replace([现有],[替新],[数量]) 默认数量值为1
012345678901234567890123456789
sr = "Life Is Short, You Need Python."
print(sr.find('o',9,25)) # 10
#若不存在 返回值 # -1
012345678901234567890123456789
sr = "Life Is Short, You Need Python."
print(sr.index('0',9,25)) # 10
#若不存在 则报错
sr = "Life Is Short, You Need Python."
print(sr.replace('Python','Money'))
# Life Is Short, You Need Money.
sr = "Life Is Short, You Need Python."
print(sr.replace('t','T',1))
# Life Is ShorT, You Need Python.
2.9字符串条件判断
.isalnum() , 判断字符串有字母或者数字组成
.isalpha() , 仅有字母
.isgidit() , 仅有数字
a = "afwtg35376dae"
b = "abc"
c = "123"
print(a.isalnum())
print(b.isalpha())
print(c.isdigit())
2.10制表符的转化
.expandtabs()
2.11字符串的分割变换
.join() , 将指定字符插入到元素之间
.split() , 以指定字符分割字符串并去除该字符
.partition() , 以指定字符分割字符串并保留该字符
#能快速将一个list变为str
li = ['i','love','python']
print(' '.join(li)) #i love python
#以指定字符将字符串切割成列表
sr = "Life Is Short,You Need Python."
print(sr.split('o',2)) #['Life Is Sh', 'rt,Y', 'u Need Python.']
#切割为元组
print(sr.partition('o')) #只能切割一次 ('Life Is Sh', 'o', 'rt,You Need Python.')
2.12ASCII值和字符的转换
chr() digit —> alpha
ord() alpha —> digit
for i in range(ord('a'),ord('z')+1):
print(chr(i),end=' ')
随机生成四位由数字,大小写英文字母组成的验证码
VCode = ''
import string
#dir(string)
#print(dir(string))
import random
#print(string.printable)
#print(string.printable.find('Z'))
#for i in range(4):
#VCode += random.choice(string.printable[0:62])
#print(VCode)
VCode = random.sample(string.printable[0:62],4)
print(''.join(VCode))