1. 字符串基础
声明字符串
s0 = "hello world" print(type(s0)) #输出为 str s1 = 'hello world' print(type(s1))#输出为 str
假如字符串中有引号符号,和 \ 符号
s2 = 'this is a "example"' print(type(s2)) #输出<class 'str'> s4 = "hello world" \ "this is china" print(type(s4), s4) #输出hello worldthis is china s5 = """ this is china. 12345 """ print(type(s5), s5) #输出三引号内的所有内容如下所示: his is china. 12345
"""
三引号声明特殊格式字符串,以及文档注释,以及类注释
"""
获取random模块的文档注释
import random print(random.__doc__)
转义字符 \会将后续内容转义
s7 = "网盘的连接地址是 \\\\192.168.10.55\\public" print(s7, type(s7)) #输出 网盘的连接地址是 \\192.168.10.55\public <class 'str'> s8 = "想要换行输入\\n就可以了" print(s8, type(s8)) #输出 想要换行输入\n就可以了 <class 'str'> s9 = "想要使用制表符输入\\t就可以了" print(s9, type(s9)) #输出 想要使用制表符输入\t就可以了 <class 'str'> s10 = "想要输入双引号输入\"就可以了" print(s10, type(s10)) #输出 想要输入双引号输入"就可以了 <class 'str'> s11 = '想要输入双引号输入\'就可以了' print(s11, type(s11)) #输出 想要输入双引号输入'就可以了 <class 'str'> s12 = "666\剑圣" print(s12, type(s12)) #输出 666\剑圣 <class 'str'>
2. 字符串遍历
字符串的拼接
s0 = "hello" s1 = "world" s2 = s0 + s1 print(s2, type(s2)) # 输出 helloworld <class 'str'> # 重复次数 s2 *= 3 print(s2, type(s2)) # 输出 helloworldhelloworldhelloworld <class 'str'>
索引:下标
s0 = "hello1world"
print(len(s0)) #输出11
print(s0[len(s0)-1])#输出d
print(s0[0], s0[5], s0[10]) # 输出h 1 d
#加上s0[11] 会有 string index out of range(字符串 索引 越界报错)
input_str = input("输入字符串") #123456
print(len(input_str), input_str[0], input_str[len(input_str)-1])
#输出6 1 6
字符串的遍历
input_str = input("输入字符串") #abcdef # 简单适合只是获取字符串内容 每一个字符 for c in input_str: print(c) #依次输出 a b c d e f length = len(input_str) # 带有索引 适合获取索引以及该索引对一个的字符 for i in range(length): print(i, input_str[i]) #依次输出 0a 1b 2c 3d 4e 5f
练习:输入一个字符串 判断字符串是否对称
例如: abcdcba 是
1234554321 是
123abab321 不是
s = "12345654321" # 分析过程如下 #s[0] == s[len(s)-1-0] #s[1] == s[len(s)-1-1] #s[2] == s[len(s)-1-2] #s[3] == s[len(s)-1-3] #s[4] == s[len(s)-1-4] for i in range(len(s) // 2): if s[i] != s[len(s)-1-i]: print("不是对称") break else: print("是对称")
3. 字符串常见操作
求字符串的长度用len()
print(len(""))
格式化字符串,默认按照顺序赋值,也可以指定参数位置
a = 10 b = 20 print(f"a={a}, b={b}")# 输出a=10 b=20 print("a={1}, b={0}".format(a, b)) #输出 a=20 b=10
split 切割
print("123456789".split("5")) # 输出['1234', '6789']
使用指定的字符串 将可迭代(可以使用for循环遍历)内容拼接
print("+".join("hello world")) # 输出h+e+l+l+o+ +w+o+r+l+d print("+".join(["1", "2", "3", "4", "5"]))# 输出1+2+3+4+5
剔除空格,也可以指定剔除指定字符
print(" 你好 ".strip()) #输出你好 print("***你好***".lstrip("*")) # 输出你好*** print("***你好***".rstrip("*")) #输出 ***你好
统计字符串出现的字数 默认到结尾 也可以指定开始 和 结束位置(不包含)
print("123123123123".count("123", 4, 9)) #输出1
默认整个字符串查找第一次出现的位置
print("23123123123".find("123", 6, 8)# 输出 -1 # 从右侧匹配第一个 print("23123123123".rfind("123")) #输出8 # 类似find 找不到直接报错 print("23123123123".index("123", 6, 8)) #输出 报错 substring not found print("23123123123".rindex("123", 6)) #输出8
print("hello world".capitalize()) #首字母大写Hello world print("hello world".upper()) #全部大写HELLO WORLD print("hello WOrld".lower())# 全部小写hello world print("hello WOrld".swapcase()) #大小写切换HELLO woRLD print("hello world".title())# 每个单词首字母都大写Hello World
在指定宽度下居中 默认使用空格也可以指定字符
print("登录".center(20, "*"))# 输出*********登录********* #居左 print("登录".ljust(20, "*"))# 输出登录****************** #居右 print("登录".rjust(20, "*"))# 输出******************登录 #居右 填充0 print("登录".zfill(20))# 输出000000000000000000登录
encode 编码格式
str_encode = "中国".encode(encoding="GB2312")#编码 print(str_encode) #输出b'\xd6\xd0\xb9\xfa' # decode 解码 print(str_encode.decode(encoding="GB2312"))#输出 中国
# startswith 开始 endswith结束
print("123456789".endswith("456")) #输出False print("123456789".startswith("123"))#输出True
replace 替换
print("abcdefg".replace("cde", "aaa"))#输出abaaafg
isalpha判断字符串是否全是字母
print("abcAZ中".isalpha())#输出True
是否全为数字
print("123a".isnumeric())#输出False
是否全为字母或数字
print("abc123".isalnum())#输出True