? 1、作用
? 2、定义
? int( ) :调用创建整型数据功能
age=10 # 本质age = int(10)
补充:
res = print(‘xxx’) #print不生产产品
print(res) #None
? 3、类型转换
? 3.1 出数字的字符串转成 int
int('1010') #1010
? 3.2 了解
? 十进制 ——>二进制
print(bin(11)) #0b1011
? 十进制——>八进制
print(oct(11)) # 0o13
? 十进制——>十六进制
print(hex(11)) # 0xb
print(hex(123)) # 0x7b
? 二进制——>十进制
print(int('0b1011',2)) # 11
? 八进制——>十进制
print(int('0o13',8)) # 11
? 十六进制——>十进制
print(int('0xb',16)) # 11
? 1、作用
? 2、定义
? float():调用创建浮点型数据的功能
salary=1.1 # 本质salary=float(1.1)
? 3、类型转换
res=float("3.1")
print(res,type(res)) #3.1 <class 'float'>
? 4、使用:
? 使用的就是数学运算 + 比较运算
? 用于记录一些描述性的信息。
? 字符串是一个值,它是有序的,不可变类型。
? 字符串的修改操作都是在原字符串的基础上新建一个字符串,并不是直接对原字符串进行修改
? 字符串定义:在单引号/双引号/三引号内包含一串字符
? 只要在引号内,空格也算字符
name1 = 'han' # 本质:name = str('任意形式内容')
name2 = "zhang" # 本质:name = str("任意形式内容")
name3 = """kang""" # 本质:name = str("""任意形式内容""")
? str可以把任意其他类型都转为字符串
res=str({'a':1})
print(res,type(res)) #{'a': 1} <class 'str'>
? 4.1 按索引取值(正向取+反向取):只能取(不能改)
? 通过索引不能修改字符串
msg='hello world'
# 正向取
print(msg[0]) #h
print(msg[4]) #o
# 反向取
print(msg[-1]) #d
msg[0] = H
print(msg) #报错, 不能通过索引改
? 4.2 切片:索引的扩展应用,从一个大字符串中拷贝出一个子字符串(顾头不顾尾,步长)
msg = 'hello world'
print(msg[0:4]) #hell
print(msg[0:5:2]) #hlo
print(msg[-1]) #d
print(msg[:]) #hello world 正着取所有
print(msg[0: -1]) #hello worl
print(msg[4:0:-1]) #olle
print(msg[4:0:-2]) #ol
print(msg[::-1]) #dlrow olleh 倒着取所有
? 4.3 统计长度 len
msg = 'hello world'
print(len(msg)) #11
? 4.4 成员运算 in 和 not in
? 判断一个子字符串是否在一个大字符串里
print("alex" in "alex is sb") #True
print("alex" not in "alex is sb") #False
print(not "alex" in "alex is sb") # False
? 4.5 移除字符串左右两侧的符号 strip
? 如果想要去掉字符串中所有符号 即 ’字符串‘.strip(’添加想要去掉的符号‘)
msg='**/*=-**egon**-=()**'
print(msg.strip('*/-=()')) #egon
? 默认去掉两侧的空格
# msg=' egon '
# res=msg.strip()
# print(msg) # 不会改变原值
# print(res) # 是产生了新值
? 了解:strip 之取两边,不取中间
msg='****egon****'
print(msg.strip('*')) #egon
msg1='****e*****gon****'
print(msg1.strip('*')) #e*****gon
? strip lstrip rstrip比较
? strip:去掉左右两侧的符号
? lstrip:去掉左侧的符号
? rstrip:去掉右侧的符号
msg='***egon****'
print(msg.strip('*')) #egon
print(msg.lstrip('*')) #egon****
print(msg.rstrip('*')) #***egon
? 4.6 切分 split
? 切分:把一个字符串按照某种分隔符进行切分,得到一个列表
? 1)默认分隔符是空格
info='egon 18 male'
res=info.split()
print(res) #['egon', '18', 'male']
? 2)指定分隔符
info='egon:18:male'
res=info.split(':')
print(res) #['egon', '18', 'male']
? 3)指定分隔次数
info='egon:18:male'
res=info.split(':',1)
print(res) #['egon', '18:male']
? 4)split 与 rsplit比较
? split :从左切
? rsplit : 从右切
info="egon:18:male"
print(info.split(':',1)) # ["egon","18:male"]
print(info.rsplit(':',1)) # ["egon:18","male"]
? 循环
info='egon:18:male'
for x in info:
print(x)
? 结果展示:
e g o n : 1 8 : m a l e
?
? 4.7 lower upper
msg='AbbbCCCC'
print(msg.lower()) #abbbcccc 全改为小写
print(msg.upper()) #ABBBCCCC 全改为大写
? 4.8 startswith endwith
print("alex is sb".startswith("alex")) #True
print("alex is sb".endswith('sb')) #True
? 4.9 format
cmd = input('请输入命令:').strip()
print('{}正在执行。。。'.format(cmd))
? 4.10 join:把列表(元素全为字符串的)拼接成字符串
? 按照某个分隔符号,把元素全为字符串的列表拼接成一个大字符串
l = ['egon', '18', 'male']
res =":".join(l) # 按照某个分隔符号,把元素全为字符串的列表拼接成一个大字符串
print(res) #egon:18:male
? 注:如果列表中有元素不是字符串的话,拼接会报错
l1=[1,"2",'aaa']
res = ":".join(l1)
print(res) #报错
? 4.11 替换 replace
msg="you can you up no can no bb"
print(msg.replace("you","YOU",)) #YOU can YOU up no can no bb
print(msg.replace("you","YOU",1))#YOU can you up no can no bb
? 4.12 isdigit
? 判断字符串是否由纯数字组成
? 只能识别 bytes(如:b‘4‘) 及 unicode(如:u‘4‘) 类型数字
print('123'.isdigit()) #True
print('12.3'.isdigit()) #False
? 补充:
num1=b'4' #bytes
num2=u'4' #unicode,python3中无需加u就是unicode
num3='四' #中文数字
num4='Ⅳ' #罗马数字
# isdigit只能识别:num1、num2
print(num1.isdigit()) # True
print(num2.isdigit()) # True
print(num3.isdigit()) # False
print(num4.isdigit()) # False
# isnumberic可以识别:num2、num3、num4
print(num2.isnumeric()) # True
print(num3.isnumeric()) # True
print(num4.isnumeric()) # True
# isdecimal只能识别:num2
print(num2.isdecimal()) # True
print(num3.isdecimal()) # False
print(num4.isdecimal()) # False
4.13 find rfind index rindex
? 1)find(‘ 字符串‘): 返回要查找的字符串在大字符串中的起始索引,如果没有会返回 -1
? 2)rfind(‘ 字符串‘): 返回要查找(从右开始查找)的字符串在大字符串中的起始索引
? 3)index(‘字符串’):返回要查找的字符串在大字符串中的起始索引,如果没有会报错
? 4)rindex(‘字符串‘): 返回要查找(从右开始查找)的字符串在大字符串中的起始索引
msg='hello egon hahaha'
print(msg.find('e')) #1 返回要查找的字符串在大字符串中的起始索引
print(msg.find('egon')) #6
print(msg.index('e')) #1
print(msg.index('egon')) #6
print(msg.find('xxx')) #返-1 代表找不到
**4.14 count**
? count:查找字符串在整个大的字符串中的个位
msg='hello egon hahaha egon、 egon'
print(msg.count('egon')) #3
**4.15 center,ljust,rjust,zfill**
#总宽度为20,字符串居中,不够用 * 补全
print('egon'.center(20,'*')) #********egon********
#总宽度为50,字符串居左,不够用 * 补全
print('egon'.ljust(20,'*')) #egon****************
#总宽度为50,字符串居右,不够用 * 补全
print('egon'.rjust(20,'*')) #****************egon
#总宽度为 10,字符串居右,
print('egon'.zfill(10)) #000000egon
? 4.16 captalize,swapcase,title
#captalize:首字母大写
print('hello world egon'.capitalize()) #Hello world egon
#swapcase:大小写翻转
print('Hello WorLd EGon'.swapcase())#hELLO wORlD egON
# title:每个单词的首字母大写
print('hello world egon'.title()) #Hello World Egon
? 4.17 is的用法
#isupper 判断是否为大写
print('abc'.isupper()) #False
#islower 判断是否为小写
print('abc'.islower()) #True
#istitle 判断字符串的单词首字母是否为大写
print('Hello World'.istitle()) #True
#isalnum():判断是否由数字,字母或者数字加字母组成
print('123aab'.isalnum()) #True
print('123'.isalnum()) #True
print('abc'.isalnum()) #True
print('123#abc'.isalnum())#False
#isalpha 判断是否全为字母
print('abc'.isalpha()) #True
print('abc1'.isalpha()) #False
#isspace 判断字符串是否全为空格
print(' '.isspace()) #True
print('ab '.isspace()) #False
#isidentifier 判断字符串命名是否规范
print('print'.isidentifier()) #True python
print('age_of_egon'.isidentifier()) #True
print('1age_of_egon'.isidentifier()) #False
? 4.18 expandtabs
? expandtabs() 方法把字符串中的 tab 符号(‘\t‘)转为空格,tab 符号(‘\t‘)默认的空格数是 8。
? 默认以 8 个空格数为一个 制表符
name ='tony\thello'
msg ='hlelo\tworld'
awp = 'withswitch'
# 设置制表符代表的空格数,默认为 8
print(msg) #hello world 用3个空格扩展
print(awp.expandtabs(4))#withswitch 没有扩展
print(name.expandtabs(1)) #tony hello 用一个空格扩展
print(name.expandtabs(2)) #tony hello 用两个空格扩展
print(name.expandtabs(3)) #tony hello 用两个空格扩展
print(name.expandtabs(4)) #tony hello 用四个空格扩展
print(name.expandtabs(5))#tony hello 用一个空格扩展
原文:https://www.cnblogs.com/xy-han/p/12459247.html