逻辑运算:
成员运算:
另外:Python的字符串与字符串不同:
# 字符串可进行加,乘(乘必须是与整数相乘) # 不可进行减,除 a = "abd" b = "abc" print(a + b) print(a * 10)
字符串
"hello world"
字符串常用功能:
- 移除空白
- 分割
- 长度
- 索引
- 切片
列表
创建列表:
1
2
3
|
name_list = [ 'alex' , 'seven' , 'eric' ] 或 name_list = list ([ 'alex' , 'seven' , 'eric' ]) |
基本操作:
- 索引
- 切片
- 追加
- 删除
- 长度
- 循环
- 包含
元祖
创建元祖:
1
2
3
|
ages = ( 11 , 22 , 33 , 44 , 55 ) 或 ages = tuple (( 11 , 22 , 33 , 44 , 55 )) |
基本操作:
- 索引
- 切片
- 循环
- 长度
- 包含
字典(无序)
创建字典:
1
2
3
|
person = { "name" : "mr.wu" , 'age' : 18 } 或 person = dict ({ "name" : "mr.wu" , 'age' : 18 }) |
常用操作:
- 索引
- 新增
- 删除
- 键、值、键值对
- 循环
- 长度
PS:循环,range,continue 和 break
Python3里一个数不管多大都是int型
Python2里分int和long型,具有范围
必须记得方法:.join .split .find strip upper lower .replace
# !/usr/bin/env python # -*- encoding:UTF-8 -*- name="王团结" if "团结" in name: print('OK') else: print("Error") name="王团结" if "王" not in name: print("1") else: print("2") a = 123 v = a.bit_length() print(v) # 强制转换 a = "123" print(type(a), a) b = int(a) print(type(b), b) #将字符串转化为数字 以base进制转换 num = "a" v = int(num, base=16) print(v) # bit_length()表示数字的二进制的位数 age = 10 r = age.bit_length() print(r) test = "aLexaLjfklasjfl" # 首字母大写 v = test.capitalize() print(v) # 所有变小写,casefold更牛逼,很多未知的对相应变小写 v1 = test.casefold() print(v1) # 针对英文 v2 = test.lower() print(v2) # 设置宽度,并将内容居中 # 20 代指总长度 # * 空白未知填充,一个字符,可有可无 v3 = test.center(20) print(v3) v4 = test.center(20, "*") print(v4) test = "alex" v = test.ljust(20, "*") print(v) test = "alex" v = test.rjust(20, "*") print(v) v = test.zfill(20) #不太好 print(v) # 去字符串中寻找,寻找子序列的出现次数 count = test.count('aL') print(count) count = test.count('aL', 4) print(count) count = test.count('aL', 5, 6) print(count) # 以什么结尾 # 以什么开头 v = test.endswith('ex') v1=test.startswith('ex') print(v) print(v1) #e xpandtabs,先断句(6,20) test = "123456 78 9" v = test.expandtabs(6) print(v, len(v)) test1 = "username email password laying ying@q.com 123 laying ying@q.com 123 laying ying@q.com 123 " v = test1.expandtabs(20) print(v) # 从开始往后找,找到第一个之后,获取位置 # 左闭右开 # > 或 >= # 字符串的第一个索引为0 v = test.find('ex') print(v) v = test.find('ex', 3, 8) print(v) #index找不到,报错,忽略 test= "alexalex" v= test.index('fdja') print(v) # 格式化,将一个字符串中的占位符({})替换为指定的值 test = 'i am {name},age {a}' print(test) v = test.format(name='alex', a=19) print(v) # 根据顺序指定对应关系(从0开始) test = 'i am {1},age {0}' print(test) v = test.format('alex', 19) print(v) #格式化,传入的值(字典){"name":'alex', "a": 19} test = 'i am {name},age {a}' v1 = test.format(name='alex', a=19) v2 = test.format_map({"name": 'alex', "a": 19}) print(v1) print(v2) print(v1==v2) #字符串中是否只包含 字母和数字 test="lkfasj123" v= test.isalnum() print(v) #是否是字母,汉字 test = "asdf" v = test.isalpha() print(v) # 当前输入是否是数字 test = "123②" v1 = test.isdecimal() # 十进制 v2 = test.isdigit() # 特殊的符号,不支持中文 v3 = test.isnumeric() # 更nb,支持中文 print(v1, v2, v3) test = "123" v1 = test.isdecimal() v2 = test.isdigit() v3 = test.isnumeric() print(v1, v2, v3) test = "123②二" v1 = test.isdecimal() v2 = test.isdigit() v3 = test.isnumeric() print(v1, v2, v3) # 大小写转换 test = "aBLex" v = test.swapcase() print(v) # 字母,数字,下划线:标识符 def class a = "def" v = a.isidentifier() print(v) # 是否存在不可显示的字符,能否打印字符 # 制表符 # 换行符 test = "oiuas fjdlsa" v = test.isprintable() print(v) #判断是否全部是空格 test = "fsd a" v = test.isspace() print(v) # 判断是否是标题,以及变成标题 test = "Return True if the string is a title-cased string, False otherwise." v = test.istitle() print(v) test = test.title() v = test.istitle() print(v) # *****将字符串中的每一个元素按照指定分隔符进行拼接 test = "你是风儿我是沙" print(test) t = '_' v = t.join(test) print(v) # 判断是否全部是大小写 和 转换大小写 test = "Alex" v1 = test.islower() v2 = test.lower() print(v1, v2) v1 = test.isupper() v2 = test.upper() print(v1, v2) # 去除空格 test = " alex " v1 = test.lstrip() v2 = test.rstrip() v3 = test.strip() print(v1) print(v2) print(v3) # 指定参数删除 test = "xalex" v1 = test.lstrip('x') v2 = test.rstrip('x') v3 = test.strip('x') print(" ") print(v1) print(v2) print(v3) # 移除指定字符串 # 有限最多匹配 test = "xalex" v1 = test.lstrip('xa') v2 = test.rstrip('9lexxexa') v3 = test.strip('xa') print(v1) print(v2) print(v3) # 替换(前提要有对应关系) v = "aisfjdslkj;jdflsakjfl;fdjsal;fj" m = str.maketrans("aeiou", "12345") new_v = v.translate(m) print(new_v) # 分隔,只有一个参数,且能拿到所有字符 test = "testfhdsajkhflkds" v1 = test.partition('s') v2 = test.rpartition('s') print(v1) print(v2) # 弊端自己处理的字符拿不到 v3 = test.split('s') v33 = test.split('s', 1) v4 = test.rsplit('s') print(v3) print(v33) print(v4) # 分割,只能根据,true,false:是否保留换行 test = "jaskldf jflsdka;dfj fjdslajf;" v = test.splitlines(True) print(v) v = test.splitlines(False) print(v) # 以XX开头,结尾 test = "backend 1.1.1.1" v1 = test.startswith('a') v2 = test.endswith('a') print(v1, v2) test = "alexafjdslalexalex" v = test.replace('ex', 'bbb') print(v) v = test.replace('ex', 'bbb',1) print(v) v = test.replace('ex', 'bbb',2) print(v)
字符串进阶
注意:len() for循环 索引 切片 基本都能用(字符串,列表等)
!/usr/bin/env python -*- encoding:UTF-8 -*- # 索引,下标,获取字符 test = "alex" v = test[0] print(v) # 左闭右开 切片 test = "alex" v = test[0:3] print(v) # 获取长度 # 汉字在python2和3有区别 test = "alex" v = len(test) print(v) # 注意 len("asdf") '_'.join("asdfs")
字符串一旦创建就不可修改和java一样