day10-11-python基础之字符串
1.开发工具
python开发ide: pycharm、eclipse
# 专业版
# 不要汉化
2.运算符
结果是值
算数运算
a = 10 * 10
赋值运算
a = a + 1 a+=1
结果是布尔值
比较运算
a = 1 > 5
逻辑运算
a = 1>6 or 1==1
成员运算
a = "蚊" in "郑建文"
3.基本数据类型
a.数字 int ,所有的功能,都放在int里
a1: int(object) 将字符串转换为数字
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 #int 4 a = "123" 5 print(type(a), a) 6 7 b = int(a) 8 print(type(b), b) 9 10 num = "0011" 11 v = int(num, base=16) 12 print(v)
a2:bit_lenght(self) 当前数字的二进制,至少用n位表示
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 #int 4 age = 5 5 r = age.bit_length() 6 print(r)
b.字符串 str
b1 capitalize() 首字母大写
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 #str 4 # 首字母大写 capitalize() 5 test = "alex" 6 v = test.capitalize() 7 print(v)
b2 casefold() 所有变小写 包括特殊字符 lower() 所有英文字母变小写
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 #str 4 # 所有变小写 casefold() lower() 5 test = "shiqianyu" 6 v1 = test.casefold() 7 print(v1) 8 v2 = test.lower() 9 print(v2)
b3 center(width, fillchar=none) width 代表总长度 fillchar 空白未知填充,一个字符,可有可无 两边填充
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 #str 4 test = 'sqy' 5 v = test.center(20,"*") 6 print(v)
b31 ljust(width, fillchar=none) width 代表总长度 fillchar 空白未知填充,一个字符,可有可无 右边填充
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 #str 4 test = "alex" 5 v = test.ljust(20,"*") 6 print(v)
b32 rjust(width, fillchar=none) width 代表总长度 fillchar 空白未知填充,一个字符,可有可无 左边填充
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 #str 4 test = "alex" 5 v = test.rjust(20,"*") 6 print(v)
b33 zfill(width) idth 代表总长度 默认左边填充0
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 #str 4 test = "alex" 5 v = test.zfill(20) 6 print(v)
b4 count(sub, start=none, end=none) 去字符串中寻找,寻找子序列的出现次数 左闭右开
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 #str 4 test = "alexalexr" 5 v = test.count('ex') 6 print(v) 7 8 test = "alexalexr" 9 v = test.count('ex',6,8) 10 print(v)
b5 以什么什么结尾 endswith(suffix, start=none, end=none)
以什么什么开始startswith(suffix, start=none, end=none)
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 #str 4 # 以什么什么结尾 5 # 以什么什么开始 6 test = "alex" 7 v = test.endswith('ex') 8 print(v) 9 v = test.startswith('alex') 10 print(v)
b6 find(sub, start=none, end=none) 从开始往后找,找到第一个之后,获取其位置 找不到返回-1
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 #str 4 # 从开始往后找,找到第一个之后,获取其未知 5 # > 或 >= 6 test = "alexalex" 7 # 未找到 -1 8 v = test.find('ex') 9 print(v)
b7 index(sub, start=none, end=none) 找不到,报错 忽略
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 #str 4 test = "alexalex" 5 v = test.index('8') 6 print(v)
b8 format(*args, **kwargs) 格式化,将一个字符串中的占位符替换为指定的值
1 # -*- coding:utf8 -*- 2 #str 3 test = 'i am {name}, age {a}' 4 print(test) 5 v = test.format(name='alex',a=19) 6 print(v) 7 8 test = 'i am {0}, age {1}' 9 print(test) 10 v = test.format('alex',19) 11 print(v)
b9 format_map(mapping) 格式化,传入的值 {"name": 'alex', "a": 19} 字典的形式
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 #str 4 # 格式化,传入的值 {"name": 'alex', "a": 19} 5 test = 'i am {name}, age {a}' 6 v1 = test.format(name='df',a=10) 7 print(v1) 8 v2 = test.format_map({"name": 'alex', "a": 19}) 9 print(v2)
b10 isalnum() 字符串中是否只包含 字母和数字 汉字
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 #str 4 # 字符串中是否只包含 字母和数字 汉字 5 test = "as123对的?" 6 v = test.isalnum() 7 print(v)
b10a isalpha() 是否是字母,汉字
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 #str 4 test = "as对的df" 5 v = test.isalpha() 6 print(v)
b10b isdecimal()<isdigit()<isnumeric() 当前输入是否是数字 判断的数字种类依次为
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 #str 4 test = "②" # 1,② ,二 5 v1 = test.isdecimal() 6 v2 = test.isdigit() 7 v3 = test.isnumeric() 8 print(v1,v2,v3)
b11 expandtabs(width) 断句 找到制表符/t 制表符两边的内容间隔的距离为width
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 #str 4 test = "username\temail\tpassword\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123" 5 v = test.expandtabs(20) 6 print(v)
b12 isprintable() 是否存在不可显示的字符 \t 制表符 \n换行符
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 #str 4 test = "oiuas\tdfkj" 5 v = test.isprintable() 6 print(v)
b13 isspace() 判断是否全部是空格
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 #str 4 test = " 1" 5 v = test.isspace() 6 print(v)
b14 istitle() 判断是否是标题 title() 转换为标题
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 test = "return true if all cased characters in s are uppercase and there is" 4 v1 = test.istitle() 5 print(v1) 6 v2 = test.title() 7 print(v2) 8 v3 = v2.istitle() 9 print(v3)
b15 join(iterable) 字符串中的每一个元素按照指定分隔符进行拼接
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 test = "你是风儿我是沙" 4 print(test) 5 # t = ' ' 6 v = "_".join(test) 7 print(v)
b16 islower() isupper() 判断是否全部是小大写 lower() upper() 转换为小大写
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 test = "alex" 4 v1 = test.islower() 5 v2 = test.lower() 6 print(v1, v2) 7 8 v1 = test.isupper() 9 v2 = test.upper() 10 print(v1,v2)
b17 strip(str) 去除两边str lstrip(str) 去除左边str rstrip(str) 去除右边str
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 # 移除指定字符串 4 # 有限最多匹配 5 test = " \nxa " 6 #v = test.lstrip('xa') 7 #v = test.rstrip('9lexxexa') 8 #v = test.strip('xa') 9 #print(v) 10 11 #test.lstrip() 12 #test.rstrip() 13 #test.strip() 14 #print(test) 15 # 去除左右空白 16 # v = test.lstrip() 17 # v = test.rstrip() 18 # v = test.strip() 19 # print(v) 20 # print(test) 21 # 去除\t \n 22 #v = test.lstrip() 23 #v = test.rstrip() 24 v = test.strip() 25 print(v)
b18 maketrans(src,dest) src源内容 dest 目标内容 对应关系替换
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 test = "aeiou" 4 test1 = "12345" 5 6 v = "asidufkasd;fiuadkf;adfkjalsdjf" 7 m = str.maketrans("aeiou", "12345") 8 new_v = v.translate(m) 9 print(new_v)
b19 分割为三部分 分割内容包含str partition(str) 从左边开始分割 rpartition(str) 从右边开始分割 分割为指定个数不包含str split(str,num) str 分割内容 num 分割 splitlines(boo) 只能根据,true,false:是否保留换行
1 #!/usr/bin/env python 2 # -*- coding:utf8 -*- 3 test = "testasdsddfg" 4 # v = test.partition('s') 5 # print(v) 6 # v = test.rpartition('s') 7 # print(v) 8 9 v = test.split('s',1) 10 print(v) 11 test.rsplit() 12 13 test = "asdfadfasdf\nasdfasdf\nadfasdf" 14 v = test.splitlines(true) 15 print(v)
进度24
c 列表 list
d 元组 tuple
e 字典 dict
f 布尔值 bool
上一篇: window.open 防止浏览器拦截
下一篇: 初识Python,简单初学代码