python数据类型
- Number-数字类型
- String-字符串
- List-列表
- Set-集合
- Tuple-元组
- Dictionary-字典
- 布尔类型bool
一.Number-数据类型
1. int类型
int类型,表示整数类型
比如1,2,3,4
python2 中分 long 型,在python3中不区分
可使用以下方式获取数据类型
>>> a=9
>>> type(a)
<class 'int'>
>>> isinstance(a,int)
True
3. float类型
常见的表现形式就是小数如4.5
4. complex类型
复数类型,一般我们都用不上的表示a+bj
二.String字符串类型
1. 字符串只能切片不能更改
2. 变量[开头下标:结束下标],切边是包头,不包尾
3. 索引下标从0开始,-1表示从最后一个开始
4. 字符串定义 单引号'hello world' 或者 双引号"hello world",
5. 三引号可支持换行,特殊字符 """hello world""" ,'''hello world''
#如果切片从0开始可以不用写0
>>> s = "helloWorld"
>>> s[0:3]
'hel'
>>> s[0]
'h'
>>> s[-1]
'd'
>>> s[:3]
'hel'
>>>
- 字符串运算符
>> a="Hello"
>>> b=" world"
>>> a +b #字符串拼接
'Hello world'
>>> a*2 #字符串重复输出
'HelloHello'
>>> a[:4]
'Hell'
>>> 'a' in a #成员运算符,在返回true
False
>>> 'a' not in a #成员运算符,不在返回true
True
>>> print("python\n")
python
>>> print(r"python\n") #原始字符串,r/R不区分大小写
python\n
>>> print(R"python\n")
python\n
>>>
-
字符串格式化
%百分号表示格式化 常用的记住%d %f %s就行了
格式化符号 | 描述 |
---|---|
%c | 格式化字符及其ASCII码 |
%d | 格式化整数 |
%e | 科学计数法格式化浮点数 |
%E | 科学计数法格式化浮点数同%e |
%f | 格式化浮点数 |
%g | %f和%e的简写 |
%G | %f 和 %E 的简写 |
%o | 无符号八进制 |
%p | 十六进制数格式化变量的地址 |
%s | 格式化字符串 |
%u | 格式化无符号整型 |
%x | 格式化无符号十六进制数 |
%X | 格式化无符号十六进制数(大写) |
-
f-string
python3.6以后新增加的比使用%百分号方便多了
>>> name = "hello"
>>> "%s world" % name
'hello world'
>>> f'{name} world'
'hello world'
>>>
-
字符串常用方法
这个几个必须要记住,endswith(),startswith(),join(),split(),count(),strip(),
>>> s = 'hello'
>>> s.capitalize() #首字母大写
'Hello'
>>> s.center(15,"*")#居中,默认是空格,后面可以加其它东西
'*****hello*****'
>>> s.count('l') #统计出现次数,跟find类似
2
>>> s.casefold() #这个不知道做什么用的,文档也没看懂
'hello'
>>> s.encode(encoding='utf-8') #字符串转换成二进制,s.decode() 二进制转字符串
b'hello'
>>> s.endswith("o") #以什么结尾,还设有s.startswith('h')以什么开头
True
>>> 'he\tll\to'.expandtabs() #分割制表符
'he ll o'
>>> s.find("e")#返回找到的次数
1
>>> s.find("l") #返回找到的次数
2
>>> "hello {0}".format("world") #格式化字符串
'hello world'
>>> s.index("l") #返回从头开始最近的一个索引
2
>>> "hello123".isalnum() #判断是否全是数字或者字母
True
>>> "hello123/".isalnum()
False
>>> s.isalpha() #判断是否全是字母
True
>>> "hello123".isalpha() #判断是否全是字母
False
>>> s.isascii() #判断是否assccii码
True
>>> s.isdigit() #判断是否数字
False
>>> s.isidentifier() 判断是否是标识符,使用这个判断是否关键字 keyword.iskeyword(s)
True
>>> 'Hello'.islower() #判断是否小写字母
False
>>> s.isprintable() #是否是可打印的,字符串都是可打印的
True
>>> s.isspace() #判断是不是空格
False
>>> 'Hello world'.istitle() #判断是不是标题,标定每个单词首字母都是大写
False
>>> s.isupper() #判断是不是大写
False
>>> '+'.join(['1','2','3'])
'1+2+3'
>>> s.ljust(7) #左对齐,可以指定宽度,默认空格 rjust(),有左就有右
'hello '
>>> s.ljust(7,'*')
'hello**'
>>> "HELLO".lower() #把大写字母转换成小写,upper() 小写转换成大写
'hello'
>>> ' hello'.lstrip() #去掉左边的空格,换行符,制表符,rstrip()去掉右边,strip()去掉两边的
'hello'
>>> s.maketrans() #这个不知道做啥用的
>>> s.partition('l')
('he', 'l', 'lo')
>>> 'hello'.replace('h','w')
'wello'
>>> s.rfind("e")
1
>>> s.rfind("l") #查找字符,返回靠近最右边的一个下标
3
>>> s.rindex('l') #跟rfind 一样
3
>>> s.rpartition('o')
('hell', 'o', '')
>>> s.rpartition('l') #从最后一个分割字符,返回一个3个字符串的元组
('hel', 'l', 'o')
>>> s.rsplit("e") #这个跟split一样功能,不知道具体用法
['h', 'llo']
>>> s.split("e") #分割字符串,
['h', 'llo']
>>>
>>>> 'ab c\n\nde fg\rkl\r\n'.splitlines() #分割各种换行,制表符等
['ab c', '', 'de fg', 'kl']
>>> 'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True)#分割各种换行,制表符等 保留
['ab c\n', '\n', 'de fg\r', 'kl\r\n']
>>> 'Hello'.swapcase() # 大写换成小写,小写换成大写
'hELLO'
>>> 'Hello world'.title() #每个首字母大写
'Hello World'
>>> s.translate('e') #这个搞不懂,做啥用的
'hello'
>>>
>>>> s.zfill(6) #指定宽度,如果不够,左边补0
'0hello'