基本类型的内置方法
数字类型
(一)整型int
#整型
# number='111'
# print(number,type(number))#111 <class 'str'>
# #1.类型强转, 符合int类型格式的字符串强转为整型。
# print(int(number),type(int(number)))#111 <class 'int'>
# #2.十进制转换为其他进制
# #十进制转化为二进制
# print(bin(1000))#0b1111101000
# #十进制转化为八进制
# print(oct(1000))#0o1750
# #十进制转化为十六进制
# print(hex(1000))#0x3e8
# #3.其他进制转为十进制
# print(int('0b1111101000',2))#1000
# print(int('0o1750',8))#1000
# print(int('0x3e8',16))#1000
(二)浮点型float
#浮点型
# #1.类型强转
# number='12.0'
# print(number,type(number))#12.0 <class 'str'>
# #类型强转, 符合浮点数类型的字符串强转为浮点型
# print(float(number),type(float(number)))#12.0 <class 'float'>
# #2.取整 round
# #四舍五入
# print(round(6.6))#7
# print(round(6.5))#6
# print(round(6.4))#6
# #3.判断数字类型 isdigit 判断是否为数字类型
# number='66' #数字
# number1=b'66' #bytes
# number2='六十六' #中文数字
# number3='Ⅳ' #罗马数字
# print(number.isdigit())#True
# print(number1.isdigit())#True
# print(number2.isdigit())#False
# print(number3.isdigit())#False
(三)字符串
字符串需要掌握的内置方法
# 字符串
# #1.索引取值
# res='hello world'
# print(res[0])#h
# print(res[-1])#d
# #字符串不允许索引修改
# res[0]='s'
r"""
Traceback (most recent call last):
File "D:\old boy\python\python28基础\day07\03字符串.py", line 7, in <module>
res[0]='s'
TypeError: 'str' object does not support item assignment
"""
# 2.字符串拼接
# # 第一种方法
# res='hello'
# res1='world'
# print(res + res1)#helloworld
# 第二种方法 join
# res='hello'
# print('.'.join(res))#h.e.l.l.o
# 3.字符串切片 切片骨头不顾尾
# res='hello world'
# print(res[0:5])#hello
# print(res[2:5])#llo
# #字符串切片支持步长 [起始位置:结束位置:步长]
# print(res[0:10:2])#hlowr
# #从指定位置切到结束
# print(res[0:])#hello world
# #从开始切到指定索引位置
# print(res[:10])#hello worl
# #从开始切到结束,指定步长
# print(res[::3])#hlwl
# res='hello world'
# #倒着取值
# # print(res[-1])#d
# # 翻转字符串
# res='hello'
# print(res[::-1])#olleh
# #4.计算长度
# res='hello world'
# print(len(res))#11
# #5.成员运算
# res='hello world'
# print('o' in res)#True
# print('s' not in res)#True
# print('s' in res)#False
# print('o' not in res)#False
# # 6.strip去除空格 lstrip去除左边空格 rstrip去除右边空格
# res = ' hello world '
#strip去除空格
# print(res.strip()) #hello world
#lstrip去除左边空格
# print(res.lstrip()) #hello world
#rstrip去除右边空格
# print(res.rstrip()) # hello world
# #两边有字符,去除指定的字符
# res='!!!hello world!!!'
# #strip去除字符
# print(res.strip('!'))#hello world
# # lstrip去除左边字符
# print(res.lstrip('!'))#hello world!!!
# # rstrip去除右边字符
# print(res.rstrip('!'))#!!!hello world
# #7.切分 split
# res='hello world'
# #split默认是按照空格进行切分
# print(res.split())#['hello', 'world']
# #split按照指定的元素切分
# res='hello|world'
# print(res.split('|'))#['hello', 'world']
# res='hello wrold'
# # split从左往有进行切分
# #split在左往右的第一个o的位置切分
# print(res.split('o',1))#['hell', ' wrold']
# #rsplit从右往左切分
# res='hello wrold'
# print(res.rsplit('o'))#['hell', ' wr', 'ld']
# # rsplit从右往左 在第一个o的位置进行切分
# print(res.rsplit('o',1))#['hello wr', 'ld']
# #8.遍历
# res='hello world'
# for i in res:
# print(i)
# h
# e
# l
# l
# o
#
# w
# o
# r
# l
# d
# #9.重复
# res='hello'
# print(res * 3)#hellohellohello
# #10.大小写转换 lower 小写 upper 大写
# res='hello WORLD'
# print(res.lower())#hello world
# print(res.upper())#HELLO WORLD
# # 11.首字母判断 startswith以什么开始 endswith以什么结尾
# res='hello world'
# print(res.startswith('hello'))#True
# print(res.startswith('ss'))#False
# print(res.endswith('world'))#True
# print(res.endswith('ss'))#False
# #12.格式化输出
# name='syh'
# age=23
# #%S %d
# print('my name is %s,my age is %s' % (name,age))
# #my name is syh,my age is 23
# #format
# print('my name is {},my age is {}'.format('syh',23))
# #my name is syh,my age is 23
# #按照索引取参数
# print('my name is {0},my age is {1}'.format('syh',23))
# #my name is syh,my age is 23
# print('my name is {0}{0}{0},my age is {1}{1}{1}'.format('syh',23))
# # my name is syhsyhsyh,my age is 232323
# #f + {}
# print(f"my name is {name},my age is {age}")
# #my name is syh,my age is 23
# #13.替换 replace
# res='hello world'
# #replace(旧的元素,新的元素)
# print(res.replace('o','s'))#hells wsrld
# #14.判断字符串类型是否符合数字类型
# input_number=input('请输入数字:')
# if input_number.isdigit():
# print(input_number,type(input_number))#11 <class 'str'>
# else:
# print('输入有误')
字符串需要了解的内置方法
# #字符串了解的内置方法
# #1.查找方法 find 顾头不顾尾
# res='hello world'
# #find 从左往右找,并切只能找到一个位置,且是第一个
# print(res.find('e'))#1
# #find 从左往右找,在指定的区间内找到的第一个,顾头不顾尾
# print(res.find('e',0,10))#1
# #rfind 从右往左找,找到的位置还是就是从左到右的正向元素的位置
# print(res.rfind('e'))#1
# #2.查找方法 index
# res='hello world'
# #index 能找到指定位置的元素
# print(res.index('d'))#10
# print(res.index('s'))#找不到的元素直接报错
r"""
Traceback (most recent call last):
File "D:\old boy\python\python28基础\day07\03字符串.py", line 169, in <module>
print(res.index('s'))
ValueError: substring not found
"""
# #3.计数count
# res='hello world'
# #统计o出现的次数
# print(res.count('o'))#2
# #4.填充
# res='syh'
# #格式center(字符串长度,填充内容)
# print(res.center(5,'-'))#-syh-
# #ljust 在字符串右边填充
# print(res.ljust(5,'-'))#syh--
# #rjust 在字符串左边填充
# print(res.rjust(5,'-'))#--syh
# #使用0进行填充,填充够指定的长度
# print(res.zfill(10))#0000000syh
# #5.制表符 \t
# res='hello\tworld'
# print(res)
# #6.首字母大写
# res='my name is syh'
# #capitalize
# #不论你是一句还 还是一个单词,只要是第一行,只有第一个单词的首字母大写
# print(res.capitalize())#My name is syh
# #title
# #可以让所有的单词首字母都大写,要符合英文语句的特点,每个单词之间要加空格
# print(res.title())#My Name Is Syh
# #7.大小写翻转 swapcase
# res='HELLO world'
# print(res.swapcase())#hello WORLD
#8.判断方法
"""
isalnum:字符串中既可以包含数字也可以包含字母
isalpha:字符串中只包含字母
islower:字符串是否是纯小写
isupper:字符串是否是纯大写
istitle:字符串中的单词首字母是否都是大写
isspace:字符串是否全是空格
isidentifier:字符串是否是合法标识符
"""
# res='Syh818'
# # 字符串中既可以包含数字也可以包含字母,True
# print(res.isalnum())
# # 字符串中只包含字母,False
# print(res.isalpha())
# # 字符串是否是合法标识符,True
# print(res.isidentifier())
# # 字符串是否是纯小写,True
# print(res.islower())
# # 字符串是否是纯大写,False
# print(res.isupper())
# # 字符串是否全是空格,False
# print(res.isspace())
# # 字符串中的单词首字母是否都是大写,False
# print(res.istitle())
(四)列表
#列表list
#1.类型强转
"""字符串、元组、字典、集合都可以强转为列表"""
# #字符串类型强转列表类型,字符串的每个元素都是列表的每个元素
# res='hello'
# print(list(res))#['h', 'e', 'l', 'l', 'o']
# #元组强转列表,元组中的每一个元素就是列表的每一个元素
# number_tuple=(1,2,3,4,5)
# print(list(number_tuple))#[1, 2, 3, 4, 5]
# #字典强转列表,字典中的键就是列表的每个元素
# dict={'name':'syh','age':23}
# print(list(dict))#['name', 'age']
# #集合强转列表,集合中的每个元素就是列表的每个元素
# set={1,2,3,4,5}
# print(list(set))#[1, 2, 3, 4, 5]
"""整型、浮点型、布尔类型都不可以转换类型"""
## 特殊range
#range生成列表
# print(list(range(1,5)))#[1, 2, 3, 4]
# #2.索引取值
# list=[1,2,3,4,5]
# print(list[0])#1
# print(list[2])#3
# #3.切片 顾头不顾尾
# list=[1,2,3,4,5]
# print(list[0:3])##[1, 2, 3]
# #支持步长
# print(list[0:4:2])#[1, 3]
# #4.计算长度
# list=[1,2,3,4,5]
# print(len(list))#5
# #5.成员运算 in / not in
# list=[1,2,3,4,5]
# print(1 in list)#True
# print(1 not in list)#False
#6.增加
#append增加 每次只能在末尾添加一个元素,
# list=[1,2,3,4,5]
# list.append('555')
# print(list)#[1, 2, 3, 4, 5, '555']
#
# #extend可以添加多个元素到列表中,添加的都是可迭代元素
# list=[1,2,3,4,5]
# #在列表中添加列表
# list.extend([66,77,88])
# print(list)#[1, 2, 3, 4, 5, 66, 77, 88]
# #在列表中添加元组
# list.extend((66,77,88))
# print(list)#[1, 2, 3, 4, 5, 66, 77, 88, 66, 77, 88]
# #在列表中添加字典
# list.extend({'name':'syh','age':18})
# print(list)#[1, 2, 3, 4, 5, 66, 77, 88, 66, 77, 88, 'name', 'age']
# #在列表中添加字符串
# list.extend('syh')
# print(list)#[1, 2, 3, 4, 5, 66, 77, 88, 66, 77, 88, 'name', 'age', 's', 'y', 'h']
#
# #insert
# #按照索引位置,在索引的位置添加元素
# list=[1,2,3,4,5]
# list.insert(1,'syh')
# print(list)#[1, 'syh', 2, 3, 4, 5]
# #7.删除
# #del
# list=[1,2,3,4,5]
# # 删除列表索引位置的元素
# del list[2]
# print(list)#[1, 2, 4, 5]
#
# #pop 弹出元素
# list=[1,2,3,4,5]
# # 默认弹出最后一个元素
# list.pop()#[1, 2, 3, 4]
# print(list)
# list.pop(0)#弹出索引位置指定的元素[2, 3, 4]
# print(list)
# #8。改值
# # 按照索引位置对指定元素进行改值。
# list = [1, 2, 3, 4, 5]
# list[2]=666#[1, 2, 666, 4, 5]
# print(list)
# #9.颠倒元素reverse
# list = [1, 2, 3, 4, 5]
# list.reverse()#[5, 4, 3, 2, 1]
# print(list)
#10.排序 sort升序 sort(reverse=True)降序 sorted()默认为升序排序
# list=[11,66,77,55,33,22,44]
# #按照升序排序
# list.sort()#[11, 22, 33, 44, 55, 66, 77]
# print(list)
# list=[11,66,77,55,33,22,44]
# #降序排序
# list.sort(reverse=True)#[77, 66, 55, 44, 33, 22, 11]
# print(list)
# # sorted()默认为升序排序
# list=[11,66,77,55,33,22,44]
# list1=sorted(list)#[11, 22, 33, 44, 55, 66, 77]
# print(list1)
# #11.迭代循环
# list=[11,22,33,44,55]
# #遍历取值
# for i in list:
# print(i)
# 11
# 22
# 33
# 44
# 55
# # 通过结合range关键字取值
# list=[11,22,33,44,55]
# for i in range(len(list)):
# print(i,end=' ')
# print(len(list))
# 0 5
# 1 5
# 2 5
# 3 5
# 4 5
# #while 循环取值
# count=0
# while count<5:
# print(count)
# count+=1
# 0
# 1
# 2
# 3
# 4
# #12.步长
# list=[1,2,3,4,5,6,7]
# #从头取到末尾,步长为3
# print(list[::3])#[1, 4, 7]
# #翻转列表
# print(list[::-1])#[7, 6, 5, 4, 3, 2, 1]