一、整形

1. base

#在16进制中的位置
num = "b"
v = int(num, base=16)
print(v) #11

2. bit_length()

# 1  1
# 2 10
# 3 11
# 4 100
# 5 101
# 当前数字的二进制,至少用n位表示
age = 5
r = age.bit_length()
print(r) #
age = 2
r = age.bit_length()
print(r) #

二、字符串

需要记住六个基本魔法:join(), split(), find(), strip(), upper(), lower()

1. find

test = "alexalex"
v = test.find('lex',3)
print(v)
从第3个位置'x'查找'lex'

2. format

test = 'i am {name}, age {a}'
print(test)
v = test.format(name='alex',a=19)
print(v) test = 'i am {0}, age {1}'
print(test)
v = test.format('alex',19)
print(v) #格式化,传入的值 {"name": 'alex', "a": 19}
test = 'i am {name}, age {a}'
v1 = test.format(name='df',a=10)
v2 = test.format_map({"name": 'alex', "a": 19})
print(v1)
print(v2)
 
3. capitalize(), upper(), casefold(), lower()
test = "aLEx"
#首字母大写
v = test.capitalize()
print(v)
#所有字母大写
v2 = test.upper()
print(v2) # 所有变小写,casefold更牛逼,很多未知的对相应变小写
v3 = test.casefold()
print(v3)
v4 = test.lower()
print(v4)

4. center

#设置宽度,并将内容居中
# 20 代指总长度
# * 空白未知填充,一个字符,可有可无
test = 'alex'
v = test.center(20,"中")
print(v) # 中中中中中中中中alex中中中中中中中中

5. count

# 去字符串中寻找,寻找子序列的出现次数
test = "aLexalexr"
v = test.count('ex')
print(v) # test = "aLexalexr"
v = test.count('ex',5,7)
print(v) #
v = test.count('ex',5,8)
print(v) #

6. endswith(), startswith()

# 以什么什么结尾
# 以什么什么开始
test = "alex"
v1 = test.endswith('ex')
print(v1) # False
v2 = test.startswith('ex')
print(v2) # True

7. isalnum(), isalpha()

# 字符串中是否只包含 字母、数字或汉字
test = "123d好"
v1 = test.isalnum()
print(v1) # True # 字符串中是否只包含 字母或汉字
v2 = test.isalpha()
print(v2) # False

8. isdecimal(), isdigit(), isnumeric()

# test = "二" # 1,② , 是否为数字
test = ''
v1 = test.isdecimal()
v2 = test.isdigit() #包含②
v3 = test.isnumeric() #包含“二”
print(v1,v2,v3) #True True True

9. isprintable()

# 是否存在不可显示的字符
# \t 制表符
# \n 换行
test = "oiuas\tdfkj"
v = test.isprintable()
print(v) # False

10. isspace()

# 15 判断是否全部是空格
test = " "
v = test.isspace()
print(v) # True

11. istitle(), title()

# 16 判断是否是标题
test = "Return True if all cased characters in S"
v1 = test.istitle()
print(v1) # False
v2 = test.title()
print(v2) # Return True If All Cased Characters In S
v3 = v2.istitle()
print(v3) # True

12. join()

# 17 ***** 将字符串中的每一个元素按照指定分隔符进行拼接
test = "你是风儿我是沙"
print(test)
t = ' '
v = t.join(test)
print(v) # 你 是 风 儿 我 是 沙

13. islower(), isupper()

# 18 判断是否全部是大小写 和 转换为大小写
test = "Alex"
v1 = test.islower()
v2 = test.lower()
print(v1, v2) # False alex v1 = test.isupper()
v2 = test.upper()
print(v1,v2) # False ALEX

14. lstrip(), rstrip(), strip()

# 移除指定字符串
test = "wxadx"
v1 = test.lstrip('x')
v2 = test.rstrip('x')
v3 = test.strip('x')
print(v1,v2,v3) # wxadx wxad wxad # 去除左右空白
v = ' df '
print(v) # ' df '
v1 = v.lstrip()
v2 = v.rstrip()
v3 = v.strip()
print(v1) # 'df '
print(v2) # ' df'
print(v3) # 'df'

15. maketrans(), translate()

# 对应关系替换
test = "aeiou"
test1 = "" v = "asidufkasd;fiuadkf;adfkjalsdjf"
m = str.maketrans("aeiou", "")
new_v = v.translate(m)
print(new_v) # 1s3d5fk1sd;f351dkf;1dfkj1lsdjf

16. partition(), rpartition()

# 21 分割为三部分
test = "testasdsddfg"
v = test.partition('s')
print(v) # ('te', 's', 'tasdsddfg')
v = test.rpartition('s')
print(v) # ('testasd', 's', 'ddfg')

17. split(), rsplit()

# 分割为指定个数
test = "testasdsddfg"
v1 = test.split('s',2)
print(v1) # ['te', 'ta', 'dsddfg']
v2 = test.rsplit('s',2)
print(v2) # ['testa', 'd', 'ddfg']

18. splitlines()

# 分割,只能根据,true,false:是否保留换行
test = "asdfadfasdf\nasdfasdf\nadfasdf"
v1 = test.splitlines(True)
print(v1) # ['asdfadfasdf\n', 'asdfasdf\n', 'adfasdf']
v2 = test.splitlines(False)
print(v2) # ['asdfadfasdf', 'asdfasdf', 'adfasdf']

19. swapcase()

# 大小写转换
test = "aLex"
v = test.swapcase()
print(v) # AlEX

20. replace()

# 将指定字符串替换为指定字符串
test = "alexalexalex"
v = test.replace("ex",'bbb')
print(v) #全部替换
v = test.replace("ex",'bbb',2)
print(v) #替换前两个

三、4个灰魔法:所有地方都能用

1. for循环

test = "郑建文妹子有种冲我来"
for item in test:
print(item)










2、索引

# 二、索引,下标,获取字符串中的某一个字符
test = '123dg'
v = test[3]
print(v) # d

3、切片

# 三、切片
test = '12df56'
v = test[0:2]
print(v) # 12d

4 、获取长度

# 四、获取长度
# Python3: len获取当前字符串中由几个字符组成
test = 'asdfg'
v = len(test)
print(v) #

四、一个深灰魔法

###################### 1个深灰魔法 ######################
# 字符串一旦创建,不可修改
# 一旦修改或者拼接,都会造成重新生成字符串

# name = "zhengjianwen"
# age = "18"
#
# info = name + age
# print(info)

day10,day11—基本数据类型语法的更多相关文章

  1. Java中的基本数据类型语法补充

    变量要先赋值后使用 不给变量赋值代表什么 不赋值就使用会怎样 (会报错) 计算并赋值运算符 作用是为了让代码更加简洁.比如 a = a + 10,可以简化为 a+=10 += -= *= /= %= ...

  2. Python3 常用数据类型语法

    1.int类型 int类型的数据是没有长度限制的,它的最大长度只与计算机的内存有关. bin(i)      返回二进制表示结果, hex(i)      十六进制, int(i)       整数( ...

  3. python基础数据类型语法

    #标识符:我们自己在写代码的时候,取的名字.#项目名 project name#包名 package name#模块名 .py ptthon文件名#规范:由字母数字下划线组成,但不能以数字#简明知意: ...

  4. Python全栈day10(基本数据类型及其常用方法)

    一,数字 1,int 将字符串转化成数字 a = '123' b=int(a) b=123 2,以进制方式进行转换 num="a" >>> num = " ...

  5. python 基础之数据类型

    一.python中的数据类型之列表 1.列表 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 二.列表常用操作 >切片>追加>插入>修改& ...

  6. 通过SQL Server自定义数据类型实现导入数据

    写在前面 在看同事写的代码时看到了SQL Server中可以自定义数据类型,而且定义的是DataTable类型的数据类型. 后我想起了以前我们导入数据时要么是循环insert写入,要么是SqlBulk ...

  7. Swift开发语法

    Swift开发入门 简介 Swift 语言由苹果公司在 2014 年推出,用来撰写 OS X 和 iOS 应用程序 2014 年,在 Apple WWDC 发布 历史 2010 年 7 月,苹果开发者 ...

  8. C语言基础语法

    #include <stdio.h> int main() { int age; printf("input your age"); scanf("%d&qu ...

  9. Swift 开发语法

    文/Tuberose(简书作者)原文链接:http://www.jianshu.com/p/5e2d4c34f18e著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 简介 Swift 语 ...

随机推荐

  1. js函数中的this关键字

    关于这个this关键字,也是很多项目中常常被用到的,那么,有人也许会问,干嘛要用this呢,在函数被调用时,直接指明是什么对象在调用不就行了?还整那么个模模糊糊的概念出来干嘛?不过嘛,存在即真理,既然 ...

  2. background-color的覆盖范围

    1. 一般div的background-color覆盖范围 到 border,margin的颜色由外层元素决定 2. body的background-color覆盖范围 到 margin,但 当htm ...

  3. 用js数组实现最原始的图片轮播实现

    上班以来看到了,写了很多的轮播图效果,实现方法大致有 1.将图片局对定位,利用z-index和opacity实现图片渐现 2.用css3 的transtion:Xx xx left/right,实现左 ...

  4. java实现报数游戏

    报数游戏 有n个孩子站成一圈,从第一个孩子开始顺时针方向报数,报到3的人出列,下一个人继续从1报数,直到最后剩下一个孩子为止.问剩下第几个孩子.下面的程序以10个孩子为例,模拟了这个过程,请完善之(提 ...

  5. java实现第三届蓝桥杯拼音字母

    拼音字母 在很多软件中,输入拼音的首写字母就可以快速定位到某个词条.比如,在铁路售票软件中,输入: "bj"就可以定位到"北京".怎样在自己的软件中实现这个功能 ...

  6. 除了FastJson,你也应该了解一下Jackson(一)

    在上月末的时候收到一条关于fastjson安全漏洞的消息,突然想到先前好像已经有好多次这样的事件了(在fastjson上面).关于安全方面,虽然中枪的机率微小,但是在这个信息越来越复杂的时代,安全性也 ...

  7. HashMap(二)之面试题系列

    定义类考题 什么是Hash?什么是HashMap? HashMap 的工作原理是什么 HashMap HashTable的区别 为什么要用HashMap 源码类考题 什么是hash碰撞,怎么减少碰撞, ...

  8. 解决intellij idea卡顿的方法

    使用idea越用越卡,即使是16G内存也是卡,多开几个微服务卡死了!! 解决方案 参考网路资源整理如下几条 1. 卸载不需要用的插件 我是Java开发,对于一些默认安装的什么安卓的google的app ...

  9. 总结:修改相关postgres用户密码

    1.修改linux系统postgres用户的密码 PostgreSQL会创建一个默认的linux用户postgres,修改该用户密码的方法如下: 步骤一:删除用户postgres的密码 sudo  p ...

  10. filebeat v6.3 多行合并的步骤 多个表达式同时匹配

    配置文件位于/etc/filebeat/filebeat.yml,就是filebeat的主配置文件打开文件filebeat.yml,搜索multiline:,默认是注释的,常用的有如下三个配置: mu ...