一、基本数据类型

1、整型(int)

ps1:
数字 int ,所有的功能,都放在int里
a1 = 123
a1 = 456

ps2:

int 将字符串转换为数字

 # -int
# 将字符串转换为数字
a = ""
b = int(a)
b = b + 1000
print(b)

执行结果:

 1123

ps3:

这种类型,不能转换

 a = "123a"   #不能转换,会报错
b = int(a)
print(b)

执行结果:

1 Traceback (most recent call last):
2 File "D:/python/day3/s4.py", line 14, in <module>
3 b = int(a)
4 ValueError: invalid literal for int() with base 10: '123a

ps4:

type 查看他是什么数据类型

 a = ""
print(type(a))
b = int(a)
print(type(b))

执行结果:

 <class 'str'>
<class 'int'>

ps5:

将字符串转换为数字

 a = ""
print(type(a),a) b = int(a)
print(type(b),b)

执行结果:

 <class 'str'> 123
<class 'int'> 123

ps6:
字符串以二进制的方式进行转换

 num = ""  #这个字符串以二进制的方式进行转换
v = int(num, base=2)
print(v)

执行结果:

3

ps7:

字符串以十六进制的方式进行转换

 num = "a"  #这个字符串以十六进制的方式进行转换
v = int(num, base=16)
print(v)

执行结果:

 10

ps8:

字符串以十六进制的方式进行转换

 num = ""  #这个字符串以十六进制的方式进行转换
v = int(num, base=16)
print(v)

执行结果:

 17

ps9:

bit_lenght
当前数字的二进制,至少用n位表示

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

执行结果:

 3

2、字符串(str)

ps1:

capitalize 首字母大写

 test = "aLex"
v = test.capitalize()
print(v)

执行结果:

Alex

ps2:

lower ,casefold

所有变小写,casefold更牛逼,很多未知的对相应变小写

 test = "aLex"
v1 = test.casefold() #casefold更牛逼,很多未知的对相应变小写
print(v1)
v2 = test.lower() #只能处理普通英文的字符,欧洲的特殊字符处理不了
print(v2)

执行结果:

 alex
alex

ps3:

3 设置宽度,并将内容居中
20 代指总长度
* 空白未知填充,一个字符,可有可无

语法:def center(self,width,fillchar=None)  #后面接参数:1、直接忽略  2、必须带  3、有等号的是可选项(可带可不带,如果不设置就用默认:None参数)

1、cneter居中 左右两边空格填充(总共长度20位,不够的左右两边空格填充)

test = "aLex"
v = test.center(20)
print(v)

执行结果:

         aLex          #总长度20位,不够左右两边空格填充

2、cneter居中 左右两边*号填充(总共长度20位,不够的左右两边*号填充)

 test = "aLex"
v = test.center(20,"*")
print(v)

执行结果:

 ********aLex********

3、center居中 左右两边中字填充(总共20位,不够的左右两边中字填充)

 test = "aLex"
v = test.center(20,"中")
print(v)

执行结果:

 中中中中中中中中aLex中中中中中中中中

4、ljust 右边以*填充(总共20位)

 test = "alex"
v = test.ljust(20,"*")
print(v)

执行结果:

 alex****************

5、rjust 左边以*填充 (总共20位)

 test = "alex"
v = test.rjust(20,"*")
print(v)

执行结果:

 ****************alex

6、zfill 左边以0填充(总共20位)

 test = "alex"
v = test.zfill(20)
print(v)

执行结果:

 0000000000000000alex

ps4:

1、count 去字符串中寻找,寻找子序列的出现次数

 test = "aLexalexr"
v = test.count('ex') #计算ex 出现的次数
print(v)

执行结果:

 2

2、count 5,6(表示起始位置,结束位置)

语法:def count(self,sub,start=None,end=None)

 test = "aLexalexr"
v = test.count('ex',5,6) #参数中的5,6表示,从那开始到那结束)
print(v)

执行结果:

 0

ps5:

encode 和 decode (这两个没有讲,先欠着,后面讲到的时候再充补)
1、encode
2、decode

ps6:

1、startswith 以什么什么开始

 test = "alex"
v = test.startswith('ex')
print(v)

执行结果:

 False

2、endswith  以什么什么结尾

 test = "alex"
v = test.endswith('ex')
print(v)

执行结果:

 True

ps7:

1、用法:expandtabs 断句 6

1 # !/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 # Author: nulige
4
5 test = "12345678\t9" #123456:6位 四个空格+78:\t补齐4位+78 9:如果后面没有6位,就不管了
6 v = test.expandtabs(6) #6就是指每6位一段,\t:就是不够六位的用空格补齐
7 print(v,len(v)) #len :判断字符串的长度

执行结果:

1 12345678    9 13

2、expandtabs,断句20,

1 # !/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 # Author: nulige
4
5 # expandtabs,断句20,
6 #输出三个\n 就换行 内容就是输出三行。
7 test = "username\temail\tpassword\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123"
8 v = test.expandtabs(20)
9 print(v)

执行结果:

1 username            email               password
2 laiying ying@q.com 123
3 laiying ying@q.com 123
4 laiying ying@q.com 123

ps8:

1、find 从开始往后找,找到第一个之后,获取其位置

> 或 >=

未找到 -1

test = "alexalex"
v = test.find('ex') #从前向后找,获取其位置
print(v)

执果结果:

 2

2、index找不到,报错 忽略

 test = "alexalex"
v = test.index('')
print(v)

执行结果:

1 Traceback (most recent call last):
2 File "D:/python/day3/s3.py", line 137, in <module>
3 v = test.index('8')
4 ValueError: substring not found

ps9:

1、format 格式化,将一个字符串中的占位符替换为指定的值

 test = 'i am {name}, age {a}'
v = test.format(name='alex',a=19)
print(v)

执行结果:

 i am alex, age 19

2、用数字,可以直接传值

 test = 'i am {0}, age {1}'
v = test.format('alex',19)
print(v)

执行结果:

 i am alex, age 

ps10:

format 格式化,传入的值 {"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)

执行结果:

 i am df, age 10
i am alex, age 19

ps11:

isalnum  判断字符串中只能包含字母和数字,如果是就是True

 test = "jjj"
v = test.isalnum()
print(v)

执行结果:

 True

ps12:

isalpha 判断字符串中是否是字母和汉子(如果不是就是Flase)

 test = "asdf"
v = test.isalpha()
print(v)
 True

ps13:

1、iddecimal ,isdigit,isnumeric 判断当前输入的值,是否是数字

 test = "②"  #这种特殊的数字
v1 = test.isdecimal() #判断是否是数字
 v2 = test.isdigit()     #判断是否是数字,还可以判断特殊的数字!  print(v1,v2)

执行结果:

 False True

2、isnumeric 判断当前输入是否是数字

 test = "二" # 1,②
v3 = test.isnumeric()
print(v3)

执行结果:

 True

ps14:

1、isidentifier 判断字母,数字,下划线:标识符:def class

 a = "_123"
v = a.isidentifier()
print(v)

执行结果:

 True

2、isidentifier 判断字母,数字,下划线:标识符:def class

 a = "def"  #标识符也符合
v = a.isidentifier()
print(v)

执行结果:

 True

3、isidentifier 判断字母,数字,下划线:标识符:def class

 a = ""  #不符合,没有下划线
v = a.isidentifier()
print(v)

执行结果:

 False

ps15:

isprintable 是否存在不可显示的字符

\t 制表符
\n 换行
 test = "oiuas\tdfkj"
v = test.isprintable()
print(v)

执行结果:

 False

ps16:

isspace 判断是否全部是空格

 test = " "
v = test.isspace()
print(v)

执行结果:

 True

ps17:

istitle, title,判断是否是标题

 test = "Return True if all cased characters in S are uppercase and there is"
v1 = test.istitle() #判断标题,首字母是否大写
print(v1)
v2 = test.title() #把普通的字符串转换为首字母大写
print(v2)
v3 = v2.istitle() #再判断v2,就是大写啦。所以是True
print(v3)

执行结果:

 False
Return True If All Cased Characters In S Are Uppercase And There Is
True

ps18:   (标记为五星表示经常用到的)

***** join 将字符串中的每一个元素按照指定分隔符进行拼接

 test = "你是风儿我是沙"
v = "_".join(test)
print(v)

执行结果:

 你_是_风_儿_我_是_沙

ps19:

判断是否全部是大小写 和 转换为大小写

1、islower 判断是否是小写 和 lower转换为小写

 test = "Alex"
v1 = test.islower() #判断是否是小写; 用于验证码:不管用户输入的是大写或小写,统一转换为小写
v2 = test.lower() #转换为小写
print(v1, v2)

执行结果:

 False ALEX

2、isupper判断是否是大写和upper 转换为大写

 test = "Alex"
v1 = test.isupper() #判断是否是大写
v2 = test.upper() #转换为大写
print(v1,v2)

执行结果:

 False ALEX

ps20:

1、lstrip,rstrip,strip去除左右空白

 test = " alex "
v1 = test.lstrip() #处理左边的空格
v2 = test.rstrip() #处理右边的空格
v3 = test.strip() #两边都处理空格
print(v1,v2,v3)

执行结果:

 alex   alex alex

2、去除\t  \n

 test = "\nlex "
v1 = test.lstrip() #处理左边的\n or \t
print(test)
print(v1)

执行结果:

     #空格
lex
lex

3、去除左边的x (移除最多字符)

 test = "xalex "
v1 = test.lstrip('x') #处理左边的x
print(v1)

执行结果:

 alex 

4、rstrip 从右边开始往左边找,先进行最多匹配

 test = "xalex"
v1 = test.rstrip('91lexex') #从右边开始往左边找,先进行最多匹配
print(v1)

执行结果:

 xa

ps21:

translate 对应关系替换

 v = "asidufkasd;fiuadkf;adfkjalsdjf"
m = str.maketrans("aeiou", "") #定义a=1,e=2,其他依此类推
new_v = v.translate(m)
print(new_v)

执行结果:

 1s3d5fk1sd;f351dkf;1dfkj1lsdjf

ps22:

1、partition,rpartition 分割为三部分

 test = "testasdsddfg"
v = test.partition('s') #找到第1个s进行分割,只能分成三部分
print(v) test = "testasdsddfg"
v = test.rpartition('s')
print(v)

执行结果:

 ('te', 's', 'tasdsddfg')  #第一个的结果
('testasd', 's', 'ddfg') #第二个的结果

2、split, rsplit 分割为指定个数,分割后s拿不到

  • 正则表达表 (以后会学)
  • 是否想要分割的元素
  • 用途:计算器
 test = "testasdsddfg"
v = test.split('s',2)
print(v) test = "testasdsddfg"
v = test.rsplit('s',2)
print(v)

执行结果:

 ['te', 'ta', 'dsddfg']
['testa', 'd', 'ddfg']

3、partition 和 split 的区别

答:区别就是分割后一个可以拿到*,一个不能拿到*

 test = "1*2"
v = test.partition('*')
print(v) test = "1*2"
v = test.split('*',2)
print(v)

执行结果:

 ('', '*', '')  #拿到了*
['', ''] #没有拿到*

ps23:

splitlines 分割,只能根据,true,false:是否保留换行

 test = "asdfadfasdf\nasdfasdf\nadfasdf"
v = test.splitlines()
print(v) test = "asdfadfasdf\nasdfasdf\nadfasdf"
v = test.splitlines(True)
print(v) test = "asdfadfasdf\nasdfasdf\nadfasdf"
v = test.splitlines(False) #参数只能加True or False
print(v)

执行结果:

 ['asdfadfasdf', 'asdfasdf', 'adfasdf']              #不加参数
['asdfadfasdf\n', 'asdfasdf\n', 'adfasdf'] #True
['asdfadfasdf', 'asdfasdf', 'adfasdf'] #False

ps24:

1、startswith 判断以xxx开头

 test = "backend 1.1.1.1"
v = test.startswith('b') #xxx开头
print(v)

执行结果:

 True

2、endswith 判断以xxx结尾

 test = "backend 1.1.1.1"
test.endswith('') #以xxx结尾
print(v)

执行结果:

 True

ps25:

swapcase 大小写转换

 test = "aLex"
v = test.swapcase()
print(v)

执行结果:

 AlEX

ps26:

1、replace 将指定字符串替换为指定字符串

 test = "alexalexalex"
v = test.replace("ex",'bbb')
print(v)

执行结果:

 albbbalbbbalbbb

2、replace 将指定字符串替换为指定字符串,指定个数

 test = "alexalexalex"
v = test.replace("ex",'bbb',2)
print(v)

执行结果:

 albbbalbbbalex

重点:

一、七个基本魔法如下:

  1. join
  2. split
  3. find
  4. strip
  5. upper
  6. lower
  7. replace

二、4个灰魔法如下:

test = "郑建文妹子有种冲我来"

1、for循环

 for 变量名 in 字符串:
变量名
break
continue index = 0
while index < len(test):
v = test[index]
print(v) index += 1
print('=======') for zjw in test:
print(zjw) test = "郑建文妹子有种冲我来"
for item in test:
print(item)
break for item in test:
continue
print(item)

2、索引,下标,获取字符串中的某一个字符

 v = test[3]

 print(v)

3、切片

 v = test[0:-1]    # 0=< <1
print(v)

4、获取长度

Python3: len获取当前字符串中由几个字符组成v = len(test)print(v)

 v = len(test)
print(v)

注意:

 len("asdf")
for循环
索引
切片

5、获取连续或不连续的数字,

 Python2中直接创建在内容中
python3中只有for循环时,才一个一个创建
r1 = range(10)
r2 = range(1,10)
r3 = range(1,10,2) 帮助创建连续的数字,通过设置步长来指定不连续
v = range(0, 100, 5) for item in v:
print(item) 练习题:根据用户输入的值,输出每一个字符以及当前字符所在的索引位置
test = input(">>>")
for item in test:
print(item) 将文字 对应的索引打印出来:
test = input(">>>")
print(test) # test = qwe test[0] test[1]
l = len(test) # l = 3
print(l) r = range(0,l) # 0,3
for item in r:
print(item, test[item]) # 0 q,1 w,2 e test = input(">>>")
for item in range(0, len(test)):
print(item, test[item])

三、1个深灰魔法

记住两句话:

  1. 字符串一旦创建,不可修改
  2. 一旦修改或者拼接,都会造成重新生成字符串
 name = "zhengjianwen"
age = ""
info = name + age
print(info)

执行结果:

 zhengjianwen18

python基础-基本数据类型总结_整型(int)_字符型(str)_day3的更多相关文章

  1. Python基础之数据类型

    Python基础之数据类型 变量赋值 Python中的变量不需要声明,变量的赋值操作既是变量声明和定义的过程. 每个变量在内存中创建,都包括变量的标识,名称和数据这些信息. 每个变量在使用前都必须赋值 ...

  2. 第二章:python基础,数据类型

    """第二章:python基础,数据类型2.1 变量及身份运算补充2.2 二进制数2.3 字符编码每8位所占的空间位一个比特,这是计算机中最小的表示单位.每8个比特组成一 ...

  3. python基础与数据类型(int, float, str, list)

    目录 python多版本共存 在cmd窗口进入不同版本的python环境 在pycharm中切换不同的版本 python语法之注释 python变量与常量 变量 变量的本质 变量的命名规范 常量 py ...

  4. python基础一数据类型之字典

    摘要: python基础一数据类型之一字典,这篇主要讲字典. 1,定义字典 2,字典的基础知识 3,字典的方法 1,定义字典 1,定义1个空字典 dict1 = {} 2,定义字典 dict1 = d ...

  5. 第一节 Python基础之数据类型(整型,布尔值,字符串)

    数据类型是每一种语言的基础,就比如说一支笔,它的墨有可能是红色,有可能是黑色,也有可能是黄色等等,这不同的颜色就会被人用在不同的场景.Python中的数据类型也是一样,比如说我们要描述一个人的年龄:小 ...

  6. python基础(二)----数据类型

    Python基础第二章 二进制 字符编码 基本数据类型-数字 基本数据类型-字符串 基本数据类型-列表 基本数据类型-元组 可变.不可变数据类型和hash 基本数据类型-字典 基本数据类型-集合 二进 ...

  7. Python学习day04 - Python基础(2)数据类型基础

    <!doctype html>day04 - 博客 figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { ...

  8. python基础之数据类型(一)

    Python3 数字(Number) 定义:a=1 特性: 1.只能存放一个值 2.一经定义,不可更改 3.直接访问 分类:整型,长整型,布尔,浮点,复数 python2.*与python3.*关于整 ...

  9. Python成长之路【第二篇】Python基础之数据类型

    阅读目录 简介 1 什么是数据? x=10,10是我们要存储的数据 2 为何数据要分不同的类型 数据是用来表示状态的,不同的状态就应该用不同的类型的数据去表示 3 数据类型 数字(整形,长整形,浮点型 ...

随机推荐

  1. netbeans打包成jar

    文件页里找到build.xml文件,打开在</project>前 加入以下代码保存之 按 Ctrl+C 复制代码 <target name="package-for-sto ...

  2. win7下Qt5使用mysql C++编程配置

    先下载mysql的库文件链接:http://files.cnblogs.com/files/xiaobo-Linux/mysql.zip 把两个文件放入 Qt目录\Qt5.5.0\5.5\mingw4 ...

  3. monkeyrunner之环境搭建及实例(三)

    Monkeyrunner工具提供了一个API,使用此API写出的程序可以在Android代码之外控制Android设备和模拟器. 一.Monkeyrunner简介 1.MOnkeyrunner相对Mo ...

  4. BestCoder Round #87 1002 Square Distance[DP 打印方案]

    Square Distance  Accepts: 73  Submissions: 598  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit ...

  5. JAVA设计模式之工厂模式

    工厂模式概念: 实例化对象,用工厂方法代替new操作 工厂模式包括工厂方法模式和抽象工厂模式 抽象工厂模式是工厂方法模式的扩展 工厂模式的意图: 定义一个接口来创建对象,但是让子类来决定哪些类需要被实 ...

  6. 解决Apache/PHP无法启动的问题

    最近经常被问到Apache无法启动的情况,所以写一篇文章,总结一下Windows下经常遇到的 Apache/PHP 无法启动的情况的解决方法. Apache/PHP 无法启动分两种情况: 1..Apa ...

  7. SOAP-XML请求(iOS应用下集成携程api)

    用携程机票为例: 携程联盟 飞机票.门票 联盟ID:278639 站点ID:739462 密钥KEY:BE57B925-E8CE-4AA2-AC8E-3EE4BBBB686F API_URL:open ...

  8. 035医疗项目-模块三:药品供应商目录模块——供货商药品目录(批量)添加药品的功能---------Service

    这篇文章我们重点介绍Service层.因为Dao层就是用Gysypml逆向生成的Mapper就可以了.所以这里重点讲解Service层. 业务逻辑如下: 1:我们从前端页面传入有两个值:1:userg ...

  9. git 保存本地更改而不需要推到远程

    git commit 修改到本地分支 repo sync . 更新分支 git checkout local 切换到本地分支 git rebase 远程 更新远程分支到本地并且将本地分支节点推到最顶

  10. sublime text2 打开包含中文的文件会自动追加.dump后缀解决办法

    用sublime text2 打开.c, .h,.txt等文件会自动追加一个.dump后缀,這样在打开.c,.h等文件时无法正常识别,从而无法正常进行语法着色,网上说是因为安装了GBK Encodin ...