一、定义:
Python中的不可变数据类型
ps:(1)字符串的长度任意,且支持重复字符
(2)字符串支持 for 和 while 循环(即支持下标索引)
(3)字符串的大小比较依据是:ASCII码值
二、常用方法:
1、大小写转换:
s1='HelloWorld'
new_s2=s1.lower()
print(s1,new_s2)
# 输出:HelloWorld helloworld
new_s3=s1.upper()
print(new_s3)
# 输出:HELLOWORLD
2、分隔:
e_mail='ysj@126.com'
lst=e_mail.split('@')
print('邮箱名:',lst[0],'邮件服务器域名:',lst[1])
# 输出:邮箱名: ysj 邮件服务器域名: 126.com
3、统计次数:
s1='HelloWorld'
print(s1.count('o')) # o在字符串s1中出现了两次
# 输出:2
4、检索:
s1='HelloWorld'
print(s1.find('o')) # o在字符串s1中首次出现的位置
# 输出:4
print(s1.find('p')) # -1,没有找到
# 输出:-1
print(s1.index('o'))
# 输出:4
#print(s1.index('p'))
# 输出:ValueError: substring not found (即子串没有找到)
5、判断前后缀:
s1='HelloWorld'
print(s1.startswith('H'))
# 输出:True
print(s1.startswith('P'))
# 输出:False
print('demo.py'.endswith('.py'))
# 输出:True
print('text.txt'.endswith('.txt'))
# 输出:True
6、替换:
s = 'HelloWorld'
new_s=s.replace('o','你好',1) # 最后一个参数是替换次数,默认是全部替换
print(new_s)
# 输出:Hell你好World
7、居中:
s = 'HelloWorld'
print(s.center(20))
# 输出: HelloWorld
print(s.center(20,'*'))
# 输出:*****HelloWorld*****
8、取除字符串:
# 去掉字符串左右的空格
s=' Hello World '
print(s.strip())
# 输出:Hello World
print(s.lstrip()) # 去除字符串左侧的空格
# 输出:Hello World
print(s.rstrip()) # 去除字符串右侧的空格
# 输出: Hello World
# 去掉指定的字符
s3='dl-Helloworld'
print(s3.strip('ld')) # 与顺序无关
# 输出:-Hellowor
print(s3.lstrip('ld'))
# 输出:-Helloworld
print(s3.rstrip('dl'))
# 输出:dl-Hellowor
9、连接字符串:
s1 = "-"
s2 = ""
seq = ("r", "u", "n", "o", "o", "b") # 字符串序列
print (s1.join( seq ))
# 输出:r-u-n-o-o-b
print (s2.join( seq ))
# 输出:runoob
三、格式化:
1、 占位符:
%s:字符串格式
%d:十进制整数格式
%f:浮点数格式
name='马冬梅'
age=18
score=98.5
print('姓名:%s,年龄:%d,成绩:%f' % (name,age ,score))
# 输出:姓名:马冬梅,年龄:18,成绩:98.500000
print('姓名:%s,年龄:%d,成绩:%.1f' % (name,age ,score))
# 输出:姓名:马冬梅,年龄:18,成绩:98.5
2、f-string:
使用 { } 标明被替换的字符(Python3.6引入的格式化字符串的方式)
name='马冬梅'
age=18
score=98.5
print(f'姓名:{name},年龄:{age},成绩:{score}')
# 输出:姓名:马冬梅,年龄:18,成绩:98.5
3、str.format() 方法:
模板字符串 .format(逗号分隔的参数)
name='马冬梅'
age=18
score=98.5
print('姓名:{0},年龄:{1},成绩:{2}'.format(name,age,score))
# 输出:姓名:马冬梅,年龄:18,成绩:98.5
print('姓名:{2},年龄:{0},成绩:{1}'.format(age,score,name))
# 输出:姓名:马冬梅,年龄:18,成绩:98.5
4、format() 详细格式控制:
s='helloworld'
print('{0:*<20}'.format(s)) # 字符串的显示宽度为20,左对齐,空白部分使用* 号填充
# 输出:helloworld**********
print('{0:*>20}'.format(s))
# 输出:**********helloworld
# 居中对齐
print('{0:*^20}'.format(s))
# 输出:*****helloworld*****
print(s.center(20,'*'))
# 输出:*****helloworld*****
# 千位分隔符(只适用于整数和浮点数)
print('{0:,}'.format(987654321))
# 输出:987,654,321
print('{0:,}'.format(987654321.7865))
# 输出:987,654,321.7865
# 浮点数小数部分的精度
print('{0:.2f}'.format(3.1419826))
# 输出:3.14
# 字符串类型 .表示是最大的显示长度
print('{0:.5}'.format('helloworld'))
# 输出:hello
# 整数类型
a=425
print('二进制:{0:b},十进制:{0:d},八进制:{0:o},十六进制:{0:x},十六进制:{0:X}'.format(a))
# 输出:二进制:110101001,十进制:425,八进制:651,十六进制:1a9,十六进制:1A9
# 浮点数类型
b=3.1415926
print('{0:.2f},{0:.2E},{0:.2e},{0:.2%}'.format(b))
# 输出:3.14,3.14E+00,3.14e+00,314.16%
四、编码与解码:
1、编码:encode() 方法
str.encode(encoding=‘utf-8’, errors=‘strict/ignore/replace’)
2、解码:decode() 方法
bytes.decode(encoding=‘utf-8’, errors=‘strict/ignore/replace’)
s='伟大的中国梦'
# 编码 str->bytes
scode=s.encode(errors='replace') # 默认是utf-8 ,因为utf-8中文占3个字节
print(scode)
# 输出:b'\xe4\xbc\x9f\xe5\xa4\xa7\xe7\x9a\x84\xe4\xb8\xad\xe5\x9b\xbd\xe6\xa2\xa6'
scode_gbk=s.encode('gbk',errors='replace') # gbk中中文占2个字节
print(scode_gbk)
# 输出:b'\xce\xb0\xb4\xf3\xb5\xc4\xd6\xd0\xb9\xfa\xc3\xce'
# 编码中的出错问题
s2='耶✌'
scode_error=s2.encode('gbk',errors='replace')
# 若是strict,则报错;若是ignore,则忽略错误;若是replace,则输出 "?"
print(scode_error)
# 输出:b'\xd2\xae?'
# 解码过程 bytes->str
print(bytes.decode(scode_gbk,'gbk'))
# 输出:伟大的中国梦
print(bytes.decode(scode,'utf-8'))
# 输出:伟大的中国梦
五、数据的验证:
1、定义:
指程序对用户输入的数据进行“合法”性验证
2、方法:
# isdigit()十进制的阿拉伯数字
print('123'.isdigit()) # True
print('一二三'.isdigit()) # False
print('0b1010'.isdigit()) # False
print('ⅢⅢⅢ'.isdigit()) # False
# 所有字符都是数字
print('123'.isnumeric()) # True
print('一二三'.isnumeric())# True
print('0b1010'.isnumeric()) # False
print('ⅢⅢⅢ'.isnumeric()) # True
print('壹贰叁'.isnumeric()) # True
# 所有字符都是字母(包含中文字符)
print('hello你好'.isalpha()) # True
print('hello你好123'.isalpha()) # False
print('hello你好一二三'.isalpha()) # True
print('hello你好ⅢⅢⅢ'.isalpha()) # False
print('hello你好壹贰叁'.isalpha()) # True
# 所有字符都是数字或字母
print('hello你好'.isalnum()) # True
print('hello你好123'.isalnum()) #True
print('hello你好一二三'.isalnum()) #True
print('hello你好ⅢⅢⅢ'.isalnum()) #True
print('hello你好壹贰叁'.isalpha()) # True
# 判断字符的大小写
print('HelloWorld'.islower()) # False
print('helloworld'.islower()) # True
print('hello你好'.islower()) # True
print('HelloWorld'.isupper()) # False
print('HELLOWORLD'.isupper()) # True
print('HELLO你好'.isupper()) # True
#所有字符都是首字母大写
print('Hello'.istitle()) # True
print('HelloWorld'.istitle()) # False
print('Helloworld'.istitle()) # True
print('Hello World'.istitle()) # True
print('Hello world'.istitle()) # False
# 判断是否都是空白字符
print('\t'.isspace()) # True
print(' '.isspace()) # True
print('\n'.isspace()) # True
六、数据的处理:
1、字符串的拼接:
s1 = 'hello'
s2 = 'world'
# (1)使用+进行拼接
print(s1+s2)
# 输出:helloworld
# (2)使用字符串的join()方法
print(''.join([s1,s2])) # 使用空字符串进行拼接
# 输出:helloworld
print('*'.join(['hello','world','python','java','php']))
# 输出:hello*world*python*java*php
print('你好'.join(['hello','world','python','java','php']))
# 输出:hello你好world你好python你好java你好php
# (3)直接拼接
print('hello''world')
# 输出:helloworld
# (4)使用格式化字符串进行拼接
print('%s%s' % (s1,s2))
# 输出:helloworld
print(f'{s1}{s2}')
# 输出:helloworld
print('{0}{1}'.format(s1,s2)) # {0},{1}为s1,s2占位
# 输出:helloworld
2、字符串的去重:
s='helloworldhelloworldadfdfdeoodllffe'
# (1)字符串拼接及not in
new_s=''
for item in s:
if item not in new_s:
new_s+=item # 拼接操作
print(new_s)
# 输出:helowrdaf
# (2)使用索引+not in
new_s2=''
for i in range(len(s)):
if s[i] not in new_s2:
new_s2+=s[i]
print(new_s2)
# 输出:helowrdaf
# (3)通过集合去重+列表排序'
new_s3=set(s)
lst=list(new_s3)
lst.sort(key=s.index)
print(''.join(lst))
# 输出:helowrdaf
微语:你簇拥热闹与盛大,余光里都是温暖与繁华。