1.字符编码
python中的字符采用的是Unicode编码
什么是编码
将字符与数字一一对应,其中字符对应的数字就是字符的编码
编码方式
ASCII码表:针对数字字符、字母字符、一些英文中常用的符号进行编码
采用一个字节对字符进行编码(2 ** 7 128个字符)
Unicode码:包含ASCII码,同时能够对世界上所有语言对应的符号进行编码
采用两个字节对字符进行编码(2 ** 15 65536个字符)
中文:4E00-9FA5
两个函数
chr(编码值) 将字符编码转换成字符
ord(字符) 获取字符的Unicode编码
将字符编码放到字符串中便是一个字符:\u + 4位的十六进制编码值
print(chr(0x4E01))
print(hex(ord('杨')), hex(ord('海')))
运行:

2.获取字符串中的字符
一旦一个字符串确定,那么字符串中每个字符的位置就确定
而且每个字符会对应一个用来表示其位置和顺序的下标值
1.下标(索引)
字符串中的每个字符都有一个下标,代表其在字符串中的位置
下标的范围是:0 ~ 字符串长度-1 (0代表从左到右第一个字符)
-1 ~ -字符串长度 (-1代表从右到左第一个字符)
2.获取单个字符
语法:
字符串[索引] 获取字符串中指定索引对应的字符 (不能越界)
说明; 字符串可以是字符串常量 也可以是字符串变量
3.获取部分字符
语法:
字符串[开始下标:结束下标:步长] (可以越界)
(开始下标默认0(-1) 结束下标默认字符串长度加1(或者相反数) 步长默认1 都可省略)
(打印结果不包含结束下标值,类型是字符串)
str1 = 'hello python'
print(str1[6], str1[-6])
print(str1[-1:5:-1])
print(str1[::-1]) # 将字符串反转
print(str1[:])
print(str1[:5:-1])
print(str1[-1::-1])
运行:

3.字符串相关运算
1.+
str1 + str2 : 将两个字符串连接在一起 产生一个新的字符串(不会改变原来的字符串)
str1 = 'abc'
str2 = '123'
print(str1 + str2, '\n', str1, str2)
运行:

2.*
str1 * n(正整数): 将字符串重复n次,产生一个新的字符串
str1 = 'abc '
print(str1 * 3)
str1 = '*'
str2 = '%'
print((str1 * 10 + str2 * 10)*3)
运行:

3.比较运算符
-
=,!=
str1 == str2
str1 != str2 -
<,><=,>= 从第一个开始,找到第一对不同的字符,然后比较他们的编码值大小
print('abc' > 'ad')
print('1ads' > 'aads')
运行:

- 判断一个字符是否是字母、中文
char = input('请输入一个字符:')
print('是否是字母:', 'a' <= char <= 'z' or 'A' <= char <= 'Z')
print('是否是中文:', '\u4e00' <= char <= '\u9fa5')
print('是否是中文:', '0x4e00' <= char <= '0x9fa5')
运行:

4.in 和 not in
字符串1 in 字符串2 判断字符串1是否在字符串2中
字符串1 not in 字符串2 判断字符串1是否不在字符串2中
print('abc' in 'abc123')
print('abc' in 'ab123c')
运行:

5.len函数
len(序列) 获取序列的长度 (一个转义字符、一个编码都为长度一)(一个空格键长度看自己电脑设置)
print(len('abc123'))
print(len('abc\n123\u4e00'))
print(len('abc\n123\\u4e00'))
print(len(' ')) (一个空格长度是自己电脑默认的空格长度)
运行:

6.str
str(数据) 将数据转换成字符串
所有类型的数据都可以转换成字符串
字符串转整型:int(字符串) 只有去掉引号后剩下的部分本身就是一个整数的字符串才能转换
字符串转浮点型:float(字符串) 只有去掉引号后剩下的部分本身就是一个浮点型的字符串才能转换
字符串转布尔型:bool(字符串) bool('0') True
0,空串 -> False 其他都是True
print(int('124'))
print(float('12.5'), float('2e3'))
运行:

4.格式字符串
1.格式字符串
指的是字符串中通过格式占位符来表示字符串中的变化,然后再通过其他的值来给占位符赋值
语法:
含有格式占位符的字符串 % (占位符对应的值, ..., ...)
说明:格式占位符 有固定的写法;可以有多个
% 固定写法
() 里面的个数和类型要和前面格式占位符一一对应
2.常见格式占位符
%d 整数
%s 字符串
%f 小数 %.nf(保留小数点后n位小数)
%c 字符 可以将数字转换成对应的字符
message = '%s今年%d岁,体重:%.2fkg,血型是:%c' % ('小明', 18, 80, 97)
print(message)
运行:

5.字符串常用的方法
字符串方法都会产生新的字符串,不会改变原来的字符串
1.字符串.capitalize() 将字符串首字符(字母)转换成大写
str1 = 'hello'
new_str = str1.capitalize()
print(new_str)
运行:

2.字符串对齐
字符串.center(width,fillchar) 居中
字符串.ljust(width,fillchar) 左对齐
字符串.rjust(width,fillchar) 右对齐
width 正整数,表示新的字符串的宽度
fillchar 字符,填充字符串
str1 = '123'
new_str = str1.center(7, '*')
print(new_str)
new_str = str1.ljust(7, '*')
print(new_str)
new_str = str1.rjust(7, '*')
print(new_str)
运行:

import random
str1 = 'python1808'
num = random.randint(0, 20)
print(num )
str2 = str(num).rjust(3, '0')
print(str1 + str2)
str1 = '23434'
print(str1.isalpha()) # 判断字符串是否全是字母
str1 = '壹23万萬43幺一百'
print(str1.isdigit()) # 判断字符串是否全是数字 文字形式不可以
print(str1.isnumeric()) # 判断字符串是否全是数字 文字形式也可以
运行:

3.join(seq)
字符串1.join(字符串2) 将字符串1插入字符串2中的每位字符之间 产生一个新的字符串
str1 = '**'
str2 = '12345'
print(str1.join(str2))
运行:

4.求字符串中最大和最小的编码值对应的字符
max(字符串)
min(字符串)
print(max('sdfj34'))
print(min('1234567wertyui'))
运行:

5.str1.count(str2) 返回str2在str1中出现的次数
str1 = 'zxcvbn123345656666'
print(str1.count('6'))
运行:

6.检查字符串开头和结尾是否是给定的字符
str1.startswith(str2,beg,end) beg,end指定范围
str1.endswith(str2,beg,end)
str1 = '123456'
print(str1.startswith('1', 2, 5))
print(str1.endswith('4', 0, 4))
运行:

7.expandtabs(tabsize=8 默认为8)把字符串中的tab符号('\t')转为空格
str1 = '123\t12345'
print(str1.expandtabs(tabsize=1))
运行:

8.find(str,beg,end)检查str是否包含在指定范围的字符串中 如果包含则返回对应的索引值,否则返回-1
str1 = '12345'
print(str1.find('7'))
运行:

9.index(str,beg,end) 跟方法find()一样,只是如果str不在字符串中会报异常
str1 = '12345'
print(str1.index('5'))
运行:

10.isalnum() 如果字符串至少有一个字符并且所有字符都是字母或者数字 则返回True,否则返回False
str1 = '12345'
str2 = '123qwe'
str3 = ''
print(str1.isalnum(), str2.isalnum(), str3.isalnum())
运行:

11.isalpha() 如果字符串至少有一个字符并且所有字符都是字母 则返回True,否则返回False
str1 = 'qwert'
str2 = '123qwe'
str3 = ''
print(str1.isalpha(), str2.isalpha(), str3.isalpha())
运行:

12.isdigit() 如果字符串至少有一个字符并且所有字符都是数字 则返回True,否则返回False(isnumerric()也可以且中文数字也行)
str1 = '1234'
str2 = '123qwe'
str3 = ''
str4 = '1234一'
print(str1.isdigit(), str2.isdigit(), str3.isdigit(), str4.isnumeric())
运行:

13.islower() 如果字符串中至少包含一个区分大小写的字符,并且所有区分大小写的字符都是小写,则返回 True 否则为False
lower() 将字符串中所有的大写字符转换成小写
str1 = 'Awert'
str2 = '123qwe'
str3 = ''
print(str1.islower(), str2.islower(), str3.islower())
print(str1.lower())
运行:

14.isupper() 如果字符串中至少包含一个区分大小写的字符并且所有区分大小写的字符都是大写,则返回 True 否则为 False
upper() 将字符串中所有的小写字符转换成大写
str1 = 'Bwert'
str2 = '123YANG'
str3 = ''
print(str1.isupper(), str2.isupper(), str3.isupper())
print(str1.upper())
运行:

15.isspace() 如果字符串中只包含空格,则返回 True,否则 返回 False
str1 = 'Bwert'
str2 = '123YANG'
str3 = ' '
str4 = ''
print(str1.isspace(), str2.isspace(), str3.isspace(), str4.isspace())
运行:

16.istitle() 如果字符串中所有的单词拼写首字母为大写,且其他字母为小写则返回 True,否则返回 False.
str1 = 'Bwert Qwwe '
str2 = '123YANG'
str3 = ''
str4 = 'BwertQwwe '
print(str1.istitle(), str2.istitle(), str3.istitle(), str4.istitle())
运行:

17.len(str1) 返回字符串str1长度
str1 = '12345'
print(len(str1))
运行:

18.lstrip() 删除字符串开头的空格或者指定字符
rstrip() 删除字符串末尾的空格或者指定字符
strip() 同时删除首尾的空格或者指定字符
str1 = ' 123 '
str2 = '122344551'
print(str1.lstrip(), str2.lstrip('1'), str2.strip('1'), str1.strip(), sep='\n')
运行:

19.maketrans(intab, outtab) 创建字符映射的转换表,对于接受两个参数的最简单的调用方式,第一个是字符串,表示需要转换的字符,第二个参数也是字符串表示转换的目标.
两个字符串的长度必须相同,为一一对应的关系。
intab -- 字符串中要替代的字符组成的字符串。
outtab -- 相应的映射字符的字符串。
translate(table, deletechars)根据str给出的表转换string的字符,要删除的字符放到 deletechars参数中
intab = "aeiou"
outtab = "12345"
trantab = str.maketrans(intab, outtab)
str = "this is string example....wow!!!"
print(str.translate(trantab))
运行:

20.replace(old, new[,max]) 将字符串中的old替换成new 如果max指定 则替换次数不超过max次
str1 = '123123123'
str2 = '1'
print(str1.replace('2', '1', 2))
运行:

21.rfind(str,beg,end) 类似find()不过是从右边开始 在返回索引 不在返回 -1
str1 = '123456'
print(str1.rfind('7'))
运行:

22.rindex(str,beg,end) 类似于 index(),不过是从右边开始.在返回索引 不在 报错
str1 = '123456'
print(str1.rindex('1'))
运行:

23.split(str,num)
str分隔符 默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。 num分割次数
通过指定分隔符对字符串进行切片 如果参数num 有指定值,则分隔 num 次
- 注意:返回的是一个列表
str1 = '1234123123'
print(str1.split('1', 2))
运行:

24.splitlines[keepends]
返回一个包含各行作为元素的列表,
keepends -- 在输出结果里是否去掉换行符('\r', '\r\n', \n'),默认 False,去掉换行符,如果为 True,则保留换行符。
- 注意:返回的是一个列表
print('ab c\n\nde fg\rkl\r\n'.splitlines())
print('ab c\n\nde fg\rkl\r\n'.splitlines(True))
运行:

25.swapcase() 将字符串中大写转换成小写 小写换大写
str1 = 'qweQWE'
print(str1.swapcase())
运行:

26.zfill(width) 返回长度为width的字符串,原来字符串右对齐 前面填充0 (width<len(str)则不变返回原来的字符串)
str1 = '1234567'
print(str1.zfill(3), len('....'))
print(str1.zfill(30))
运行:

27.isdecimal() 检查字符串是否只包含十进制字符,如果是返回 true,否则返回 false。
str1 = '1242345'
str2 = '1230o123'
print(str1.isdecimal(), str2.isdecimal())
运行:

网友评论