浅析Python数字类型和字符串类型的内置方法
作者:Python热爱者 时间:2022-06-16 12:57:24
一、数字类型内置方法
1.1 整型的内置方法
作用
描述年龄、号码、id号
定义方式
x = 10
x = int('10')
x = int(10.1)
x = int('10.1') # 报错
内置方法
没有内置方法,只有算术运算和比较运算
存在一个值还是多个值
存一个值
有序or无序 有序:有索引;无序:无索引
压根就没有这一说
可变or不可变(重点)
数字类型不可变
可变(变量值而言)
lis = [1,2,3]
print(id(lis))
lis[0] = 2 #lis-->[2,2,3]
print(id(lis))
不可变(变量值而言),值变id也变
x = 10
print(id(x))
x = 20
print(id(x))
1.2 浮点型内置方法
同整型
二、字符串类型的内置方法
2.1 作用
姓名/性别/地址
2.2 定义方式
s = b‘sdkfljl' # 打印出来的bytes类型,二进制类型,010011001011001011
print(s)
print(‘中文',encode(‘utf8'))
2.3 字符串内置方法(只有字符串类型才能使用)
s = ‘forever handsome'
优先掌握(今天必须得掌握)
索引取值
print(s[1])
索引切片
print(s[4:0:1]) # 1 表示从左到右
print(s[-4:0:-1]) # -1 表示从右到左
for循环
for i in s:
print(i)
strip() 去除两端指定字符
s1 = ‘ nick handsome '
print(s1.strip()) # 去除两端的空白
s2 = '****!!!nick handsome-----***'
print(s2.strip('-*!')) # 指定多个字符一起去掉,只能strip里面有的字符就全部干掉
split() 切割
print(s.split()) # 默认以空格为切割条件
print(s.split('/')) # 以/切割
print(s.split('!')) # 以!切割
in 或 not in
print('forever' in s ) #True
print('!' not in s) # True
长度len
s = '123'
print(len(s)) # 3 # 求字符串的长度
2.4 存一个值还是多个值
一个值
2.5 有序or无序
有序
2.6 可变or不可变(重点)
可变:值变id不变,不可哈希
不可变:值变id也变,可哈希
不可变
s2 = 'abc'
print(id(s2))
s2 += 'abc'
print(id(s2))
总结
以上所述是小编给大家介绍的Python数字类型和字符串类型的内置方法,希望对大家有所帮助!
来源:https://blog.51cto.com/14246112/2460659
标签:Python,数字类型,字符串类型,内置
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
一行代码生成Tableau可视化图表的方法
2022-09-21 12:01:47
![](https://img.aspxhome.com/file/2023/6/117946_0s.png)
Mysql数据库高级用法之视图、事务、索引、自连接、用户管理实例分析
2024-01-15 20:17:00
![](https://img.aspxhome.com/file/2023/4/77204_0s.png)
Python获取当前时间日期的实现示例
2021-12-26 04:43:21
asp使用 sql_dmo 给表添加索引
2010-03-17 20:57:00
python的几种开发工具介绍
2021-09-28 01:54:44
Python实现归一化算法详情
2023-05-11 12:18:33
![](https://img.aspxhome.com/file/2023/9/94019_0s.png)
对PyQt5中树结构的实现方法详解
2021-02-07 16:19:20
![](https://img.aspxhome.com/file/2023/3/105283_0s.jpg)
node.js express框架简介与实现
2024-05-08 09:35:22
![](https://img.aspxhome.com/file/2023/0/129470_0s.png)
在pytorch中实现只让指定变量向后传播梯度
2022-03-27 15:35:30
![](https://img.aspxhome.com/file/2023/2/117892_0s.jpg)
PHP 引用的概念
2023-11-14 21:24:28
js实现点击图片自动提交action的简单方法
2024-04-29 14:07:21
golang 各种排序大比拼实例
2024-05-21 10:18:27
Python实现TOPSIS分析法的示例代码
2021-05-09 19:32:47
![](https://img.aspxhome.com/file/2023/1/109621_0s.png)
提高javascript的速度系列(序)
2008-07-15 12:38:00
SQL Server实现分布式数据库系统的终极目标
2010-08-05 14:57:00
python将图片转base64,实现前端显示
2021-04-26 14:03:04
![](https://img.aspxhome.com/file/2023/0/97290_0s.jpg)
高效管理http连接的方法
2022-02-16 23:12:14
![](https://img.aspxhome.com/file/2023/5/132505_0s.png)
Win2003服务器安装及设置教程 MySQL安全设置图文教程
2024-01-28 17:55:46
![](https://img.aspxhome.com/file/2023/4/104434_0s.gif)
vue项目中使用this.$confirm解析
2024-05-02 17:02:09
![](https://img.aspxhome.com/file/2023/3/130083_0s.png)
Ubuntu手动安装mysql5.7.10
2024-01-16 11:53:56
![](https://img.aspxhome.com/file/2023/2/79012_0s.jpg)