int、bool和str
int
bit_length
返回以二进制表示的最短长度
print(int.bit_length(10))
结果
4 Process finished with exit code 0
int()
将字符串,布尔值,浮点数转换成了整数;将二进制、八进制、十六进制转换成十进制
1. 转换字符串
print(int(""), int(True))
运行结果
256 1 Process finished with exit code 0
int()不仅可以将“123”这种形式的字符串转换成数字,还可以将英文字母组成的字符串转换成数字,官方文档是这样阐述的
int(x, base)
Convert a number or string to an integer, or return 0 if no arguments
are given. If x is a number, return x.__int__(). For floating point
numbers, this truncates towards zero. If x is not a number or if base is given, then x must be a string,
bytes, or bytearray instance representing an integer literal in the
given base. The literal can be preceded by '+' or '-' and be surrounded
by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.
Base 0 means to interpret the base from the string as an integer literal.
>>> int('0b100', base=0)
# copied from python office document
来看一段代码
print(int("0b101", 0))
print(int("a", 11))
print(int("b0", 12))
print(int("abc", 15))
运行结果
5
10
132
2427 Process finished with exit code 0
当base=0时,表示将二进制的数转换成十进制
int("abc",15)表示将以15进制表示的abc转换成10进制的数字
2. 转换bool值
int(True)
1
int(False)
0
3. int()转换浮点数时是向0靠近,而不是四舍五入
int(4.6)
4
int(-2.7)
-2
4. 将二进制、八进制、十六进制转换成十进制
print(int(0b10)) # 二进制前缀0b
print(int(0o10)) # 八进制前缀0o
print(int(0x12)) # 十六进制前缀0x
运行结果
2
8
18 Process finished with exit code 0
str
切片
字符串的操作如切片等都是在内存中重新开辟一个空间放置这个新的字符串,新的字符串与原来的字符串没有半毛钱关系
切片这里有一个坑:当步长为负值时,返回的字符串是倒序排列的,来看代码
s = "小白兔啊白又白两只耳朵竖起来"
print(s[-3:-9:-2])
输出结果
竖耳两 Process finished with exit code 0
如果索引不写的话可以是开头也可以是结尾(步长为负时)
s = "小白兔啊白又白两只耳朵竖起来"
s1 = s[:5:-1]
print(s1)
运行结果
来起竖朵耳只两白 Process finished with exit code 0
因为步长为-1,所以默认不写的索引是结尾而不是开头
.capitalize()
将首字母大写,如果首字母已经是大写或者首字母不是字母则返回原值不报错
.center(length,fillchar)
length为总长度,fillchar为填充符,不写填充符则系统默认用空格填充
s = "hello"
print(s.center(50, "$"))
运行结果
$$$$$$$$$$$$$$$$$$$$$$hello$$$$$$$$$$$$$$$$$$$$$$$ Process finished with exit code 0
验证默认填充符是空格
s = "hello"
s1 = s.center(50)
s2 = s1[0:5]
print(s2.isspace())
运行结果
True Process finished with exit code 0
.swapcase()
大小写反转,如果字符串里没有字母返回原值不报错
.title()
以非英文字母(特殊字符或中文)隔开的部分的首字母大写
.upper()
全部大写,用于验证码
.lower()
全部小写
.startswith()
判断是否以括号里的字符开头(可以是单个字符或者几个字符),返回True或者False
.endswith()
判断是否以括号里的字符结尾
.find(a)
返回第一个a的索引,找不到返回-1
.index(a)
返回第一个a的索引,找不到报错
.strip()
默认去掉前后两端的空格、制表符和换行符,用于输入时去掉那些字符;括号内有参数时去掉所有参数里包含的单个字符(把参数拆成最小的字符);lstrip()去掉左边,rstrip()去掉右边
s = "\thello world "
print(s.strip())
print(s.lstrip())
print(s.rstrip())
运行结果
hello world
hello world
hello world Process finished with exit code 0
strip()括号里可以传入字符,表示去掉这些字符,如果没有指定的字符则不变,不报错
s = "\thello world "
print(s.strip("\t"))
print(s.lstrip(" "))
print(s.rstrip("d"))
运行结果
hello world
hello world
hello world Process finished with exit code 0
.split()
默认以空格为依据分割,也可以以传入的字符分割,返回列表。字符串----->列表
看strip的源码:
Return a list of the words in S, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are
removed from the result.
1. 不指定分隔符
s = " shkdf hsab\tkdsf k rg\nba "
print(s.split())
执行结果
['shkdf', 'hsab', 'kdsf', 'k', 'rg', 'ba'] Process finished with exit code 0
2. 指定分隔符
s = "adfdkhdahfacjjadja "
print(s.split("a"))
运行结果
['', 'dfdkhd', 'hf', 'cjj', 'dj', ' '] Process finished with exit code 0
3. 指定最大分割次数(从左边开始)
s = "adfdkhadahfacjjadja "
print(s.split("a", 2))
运行结果
['', 'dfdkh', 'dahfacjjadja '] Process finished with exit code 0
可以看到分割了两次,结果列表里包含3个元素
4. .rsplit() 从右边开始分割
s = "adfdkhadahfacjjadja "
print(s.split("a", 2))
print(s.rsplit("a", 2))
执行结果
['', 'dfdkh', 'dahfacjjadja ']
['adfdkhadahfacjj', 'dj', ' '] Process finished with exit code 0
小结:
(1)默认从左边开始分割,如果要从右边开始用.rsplit()
(2)如果不指定分隔符,所有的空格符、缩进符和换行符都会作为分割符来分割字符串,返回的列表会自动去掉空字符;
(3)如果指定分隔符,且分隔符在两端,返回的列表里会包括空字符(深坑请注意)
.join()
列表-------->字符串(前提是列表元素都是字符串)
来看一个示例:
lst = ["apple", "orange", "pineapple"]
s = "-"
print(s.join(lst))
执行结果如下:
apple-orange-pineapple Process finished with exit code 0
join()括号里也可以是字符串等其他可迭代对象
s1 = "love"
print("❤".join(s1))
执行结果
l❤o❤v❤e Process finished with exit code 0
.replace(old,new,count)
默认全换,写上count换count个
s = "my love"
print(s.replace("o", "❤"))
运行结果
my l❤ve Process finished with exit code 0
replace()还可以加上次数,默认从左到右
s = "吃葡萄不吐葡萄皮不吃葡萄倒吐葡萄皮"
print(s.replace("葡萄", "grape", 3))
运行结果
吃grape不吐grape皮不吃grape倒吐葡萄皮 Process finished with exit code 0
小结
以上几种方法中用的较多的有find、index、strip和title,find和index用于确定元素的索引,strip用于去掉空格等特殊字符,title用于首字母大写。
.format()
格式化输出
第一种方式:相当于位置参数,按照顺序来
# 第一种形式
s = "我叫{},今年{}岁,性别{},爱好{}".format("王大锤", 22, "男", "女")
print(s)
运行结果
我叫王大锤,今年22岁,性别男,爱好女 Process finished with exit code 0
第二种方式:给参数标上序号,可以重复利用
# 第二种形式
s = "我叫{0},今年{1}岁,性别{2},我{0}最大的爱好就是{3}".format("王大锤", 22, "男", "妹子")
print(s)
运行结果
我叫王大锤,今年22岁,性别男,我王大锤最大的爱好就是妹子 Process finished with exit code 0
第三种形式:关键字参数
# 第三种形式
s1 = "我叫{name},今年{age}岁,性别{gender},爱好{hobby},我{name}又回来了".format(age=22, gender="男", name="王大锤", hobby="女")
print(s1)
运行结果
我叫王大锤,今年22岁,性别男,爱好女,我王大锤又回来了 Process finished with exit code 0
.is方法
isdigit() 是否全为数字,不包括小数,返回True或者False
isalphanum() 是否为字母或数字,返回True或者False
isalpha() 是否全为字母,返回True或者False
isspace() 是否全为空格、制表符或换行符,是返回True否则返回False
islower() 有英文字母(不管还有没有其他字符)且为小写返回True,否则返回False
isupper() 有英文字母(不管还有没有其他字符)且为大写返回True,否则返回False
istitle() 是否是tilte格式(即所有被特殊字符隔断的部分首字母大写)
.count()
计算某个元素出现的次数
s = "吃葡萄不吐葡萄皮不吃葡萄倒吐葡萄皮"
print(s.count("葡萄"))
运行结果
4 Process finished with exit code 0
len()
len为内置函数,写法为len(s),作用是计算字符串的长度
int、bool和str的更多相关文章
- Python基础数据类型之int、bool、str
数据类型:int bool str list 元祖 dict 集合 int:整数型,用于各种数学运算. bool:只有两种,True和False,用户判断. str:存储少量数据,进行操作 ...
- day03 int bool str
1. 昨日内容回顾 1. while循环 语法: while 条件: 循环体 else: 语句块 执行过程:判断条件是否为真. 如果真, 执行循环体.然后再次判断条件... 直到条件为假循环停止 br ...
- 关于容器类型数据的强转一共:str() list() set() tuple() dict() 都可以转换成对应的数据类型 /Number 数据类型的强转一共: int() bool() flaot() complex() 都可以转换成对应的数据类型
# ###强制转换成字典类型 # 多级容器数据:该类型是容器数据,并且里面的元素还是容器类型数据 # ###二级容器 # 二级列表 listvar = [1,3,4,5,[6,7,8,9]] res ...
- 基本数据类型int,bool,str
.基本数据类型(int,bool,str) 基本数据数据类型: int 整数 str 字符串. 一般不存放大量的数据 bool 布尔值. 用来判断. True, False list 列表.用来存放大 ...
- 三.int , bool , str
03.万恶之源-基本数据类型(int, bool, str) 本节主要内容: 1. python基本数据类型回顾 2. int----数字类型3. bool---布尔类型4. str--- 字符串类 ...
- 第三天-基本数据类型 int bool str
# python基础数据类型 # 1. int 整数 # 2.str 字符串.不会用字符串保存大量的数据 # 3.bool 布尔值. True, False # 4.list 列表(重点) 存放大量的 ...
- 基本数据类型(int,bool,str)
目录: 1.int 数字类型 2.bool 布尔值 3.str 字符串类型 一.整型(int) 在python3中所有的整数都是int类型.但在python2中如果数据量 ...
- Python中int,bool,str,格式化,少量is,已经for循环练习
1.整数 十进制转化为二进制 xxx.bit_length(). #求十进制数转化成二进制时所占用的位数 2.布尔值 bool # 布尔值 - - 用于条件使用 True 真 Fa ...
- 基本数据类型(int,bool,str)
1.int bit_lenth() 计算整数在内存中占用的二进制码的长度 十进制 二进制 长度(bit_lenth()) 1 1 1 2 10 2 4 100 3 8 1000 4 16 10000 ...
- python-基本数据类型(int,bool,str)
一.python基本数据类型 1. int ==> 整数. 主要⽤用来进⾏行行数学运算 2. str ==> 字符串串, 可以保存少量量数据并进⾏行行相应的操作 3. bool==> ...
随机推荐
- Freescale 车身控制模块(BCM) 解决方案
中国汽车业已成为全球第一市场,标志着中国汽车产业进入了白热化竞争时代,因此,人们对汽车的操控性,安全性,易用性,舒适性,以及智能化要求也越来越高,更大的空间需求和更多的零部件因而产生了冲突,这就要求汽 ...
- 我们如何用Go来处理每分钟100万复杂请求的场景
在Malwarebytes我们经历了显著的增长,自从我一年前加入了硅谷的公司,一个主要的职责成了设计架构和开发一些系统来支持一个快速增长的信息安全公司和所有需要的设施来支持一个每天百万用户使用的产品. ...
- 代码合并工具Beyond Compare的使用技巧
使用技巧 文件合并 过滤 https://edi.wang/post/2013/2/17/using-beyond-compare 文件夹比较 http://www.beyondcompare.cc/ ...
- Django(十九)Ajax全套
参考博客:http://www.cnblogs.com/wupeiqi/articles/5703697.html 提交: - Form - Ajax 一.Ajax,偷偷向后台发请求 - XMLHtt ...
- mybatis 插入一条记录 参数为map的写法
xml: <insert id="insertUser" parameterType="java.util.Map"> insert into us ...
- JMeter-Java压力测试工具-01
先去官网下载 打开 下面测试一个小栗子 建立一个工程,提供一个查询接口 package com.example.demo; import org.springframework.web.bind.an ...
- vue,elementUI,less,axios,qs的安装及打包
目前一直使用vue去搭建项目,我个人习惯用Visual Studio Code编辑工具,所以下面的所有操作都是在这个编辑器中 在安装的时候,为了避免安装过程中出错,你最好安装了淘宝镜像(官网:http ...
- C# string.format用法详解
String.Format 方法的几种定义: String.Format (String, Object) 将指定的 String 中的格式项替换为指定的 Object 实例的值的文本等效项. Str ...
- Mac 软件专题:高效率工作和学习工具软件推荐
今天和大家分享软件专题:「高效率工作和学习工具」,简而言之就是提高你工作和学习效率的软件,这对于要天天使用Mac工作或学习的人来说太有帮助了,这里主要分享大家平时经常用的一些,欢迎留言补充. 本文图片 ...
- bzoj1061 建图 + 最小费用流
https://www.lydsy.com/JudgeOnline/problem.php?id=106152 对于一个点对上多个点,不太容易建图的时候,考虑逆向思考 申奥成功后,布布经过不懈努力,终 ...