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的更多相关文章

  1. Python基础数据类型之int、bool、str

    数据类型:int  bool  str  list  元祖  dict  集合 int:整数型,用于各种数学运算. bool:只有两种,True和False,用户判断. str:存储少量数据,进行操作 ...

  2. day03 int bool str

    1. 昨日内容回顾 1. while循环 语法: while 条件: 循环体 else: 语句块 执行过程:判断条件是否为真. 如果真, 执行循环体.然后再次判断条件... 直到条件为假循环停止 br ...

  3. 关于容器类型数据的强转一共:str() list() set() tuple() dict() 都可以转换成对应的数据类型 /Number 数据类型的强转一共: int() bool() flaot() complex() 都可以转换成对应的数据类型

    # ###强制转换成字典类型 # 多级容器数据:该类型是容器数据,并且里面的元素还是容器类型数据 # ###二级容器 # 二级列表 listvar = [1,3,4,5,[6,7,8,9]] res ...

  4. 基本数据类型int,bool,str

    .基本数据类型(int,bool,str) 基本数据数据类型: int 整数 str 字符串. 一般不存放大量的数据 bool 布尔值. 用来判断. True, False list 列表.用来存放大 ...

  5. 三.int , bool , str

     03.万恶之源-基本数据类型(int, bool, str) 本节主要内容: 1. python基本数据类型回顾 2. int----数字类型3. bool---布尔类型4. str--- 字符串类 ...

  6. 第三天-基本数据类型 int bool str

    # python基础数据类型 # 1. int 整数 # 2.str 字符串.不会用字符串保存大量的数据 # 3.bool 布尔值. True, False # 4.list 列表(重点) 存放大量的 ...

  7. 基本数据类型(int,bool,str)

    目录: 1.int        数字类型 2.bool      布尔值 3.str    字符串类型 一.整型(int) 在python3中所有的整数都是int类型.但在python2中如果数据量 ...

  8. Python中int,bool,str,格式化,少量is,已经for循环练习

    1.整数 ​ 十进制转化为二进制 ​ xxx.bit_length(). #求十进制数转化成二进制时所占用的位数 2.布尔值 ​ bool # 布尔值 - - 用于条件使用 ​ True 真 ​ Fa ...

  9. 基本数据类型(int,bool,str)

    1.int bit_lenth() 计算整数在内存中占用的二进制码的长度 十进制 二进制 长度(bit_lenth()) 1 1 1 2 10 2 4 100 3 8 1000 4 16 10000 ...

  10. python-基本数据类型(int,bool,str)

    一.python基本数据类型 1. int ==>  整数. 主要⽤用来进⾏行行数学运算 2. str ==> 字符串串, 可以保存少量量数据并进⾏行行相应的操作 3. bool==> ...

随机推荐

  1. 【POJ 1001】Exponentiation (高精度乘法+快速幂)

    BUPT2017 wintertraining(15) #6A 题意 求\(R^n\) ( 0.0 < R < 99.999 )(0 < n <= 25) 题解 将R用字符串读 ...

  2. Hdoj 2109.Fighting for HDU 题解

    Problem Description 在上一回,我们让你猜测海东集团用地的形状,你猜对了吗?不管结果如何,都没关系,下面我继续向大家讲解海东集团的发展情况: 在最初的两年里,HDU发展非常迅速,综合 ...

  3. Android LinearLayout 渐变背景

    更多  参考 Drawable Resources | Android Developers main_header.xml: <?xml version="1.0" enc ...

  4. [HNOI2015]菜肴制作(拓扑排序)

    知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴. ATM 酒店为小 A 准备了 N 道菜肴,酒店按照为菜肴预估的质量从高到低给予1到N的顺序编号,预估质量最高的菜肴编号为1. 由于菜肴之间口味搭 ...

  5. 构造器引用和直接用new创建对象区别

    万事用事实说话 package cn.lonecloud; /** * @author lonecloud * @version v1.0 * @date 上午11:22 2018/4/30 */ p ...

  6. 区块链使用Java,以太坊 Ethereum, web3j, Spring Boot

    Blockchain is one of the buzzwords in IT world during some last months. This term is related to cryp ...

  7. ecplise 修改编码

    1.修改eclipse默认工作空间编码方式 window->preferences->general->workspace 2.修改工程编码方式 项目右键->propertie ...

  8. [luogu1552][派遣]

    题目链接 思路 首先肯定要树形dp,一直没想到怎么用左偏树.如果不断弹出又不断地合并复杂度不就太高了.瞄了眼题解才知道可以直接用大根树.然后记录出当前这棵左偏树的大小(树里面所有点的薪水之和)以及点的 ...

  9. poj 1523"SPF"(无向图求割点)

    传送门 题意: 有一张联通网络,求出所有的割点: 对于割点 u ,求将 u 删去后,此图有多少个联通子网络: 对于含有割点的,按升序输出: 题解: DFS求割点入门题,不会的戳这里

  10. PHP的简单工厂模式

    又称为静态工厂方法(Static Factory Method)模式,它属于类创建型模式.在简单工厂模式中,可以根据参数的不同返回不同类的实例.简单工厂模式专门定义一个类来负责创建其他类的实例,被创建 ...