Python字符串和整数常用功能总结

一、 整数类型                                                                                                               

变量类型 需求描述 代码解释 实现代码举例
Int 将字符串转换为数字的方法 将字符串或浮点数转换为整数数类型 n = "3"    m = int(n)    print(type(n),n)   print(type(m),m)
Int 将数字按照进制转换 base=16代表转换为16进制数,a是指的原来的变量名; a = "1011"    x = int(a,base=16)    print(x)
Int  查看当前数字所占的比特位数   x = 10    y = x.bit_length()    print(y)

 二、字符串类型

变量类型 需求描述 右列代码的解释 实现代码举例
Str 首字母大写   v = "hello"     x = v.capitalize()    print(x)
Str 字母大写变小写-1 包含常见和不常见字符; v = "heLlO"    x = v.casefold()    print(x)
Str 字母大写变小写-2 包含常见字符; v = "heLlO"    x= v.lower()    print(x)
Str 判断字母是否为小写字母   v = "heLlO"    x= v.islower()    print(x)
Str 字母小写变大写   v = "heLlO"    x= v.upper()    print(x)
Str 判断字母是否为大写字母   v = "heLlO"    x= v.isupper()    print(x)
Str  设置宽度并将内容居中 20代表内容所占宽度,*位置只能用一个字符,不能是数字,但是可有可无;  v = "Hello"     x = v.center(20,"*")    print(v)  
Str  计算字符或子序列出现多少次 L位置要统计的字符或者子序列, 5,6代表要统计的始末位置;  v = "heLlO"    x = v.count("L",5,6)    print(x) 
Str  检查以什么为结尾 O位置代表结尾要审核的字符; v = "heLlO"    x = v.endswith('O')    print(x)
Str 检查以什么为开头 h位置代表开头要审核的字符; v = "heLlO"    y = v.startswith('h')   print(y) 
Str  查找子序列的位置-1 从开始往后找,找到第一个就获取其位置,并终止查找; v = "012312"  x = v.find('12')        print(x)
Str 查找子序列的位置-2 从指定位置找,此功能的前边位置为开区间>=,后边位置为闭区间<; v = "012312"  y = v.find('12'4,6)   print(y)
Str  查找子序列的位置-3 index与find不同的是:find没找到显示-1,index查找不到会报错;  v = "01632"    x = v.index('9')       print(x)
Str  格式化将字符串中的占位符替换为指定的值-1   test = "i am {name}",age(a)     i = test.format(name="tom",a=19)   print(i) 
Str  格式化将字符串中的占位符替换为指定的值-2   test = "i am {0}",age(1)         i = test.format("tom",19)         print(i)
Str  格式化将字符串中的占位符替换为指定的值-3   test = "i am {name}",age(a)   i = test.format_map({"name":"tom","a":19})    print(i)
Str 判断字符串中是否只是包含的字母和数字   test = "123abc"   v = test.isalnum()   print(v) 
  判断字符串中是否只是包含字母   test = "123abc"   v = test.isalpha()   print(v) 
Str  调整列宽为指定长度,常用语做表格  \t 制表符  \n 换行符 test = "123\tabc\tcdd\t\n123\tabc\tcdd\t"   v = test.expandtabs(20)   print(v)
Str  判定输入的是否是数字 不支持识别特殊格式的数字  a = "123"  x = a.isdecimal()   print(x)
Str  判定输入的是否是数字 支持识别特殊格式的数字 a = "123"  x = a.isdigit()   print(x)
Str  判定输入的是否是数字 支持识别特殊格式的数字和中文数字 a = "123"  x = a.isnumeric()   print(x) 
Str  判断输出的内容是否有不可显示的字符   a = "123/t"  x= a.printable()   print(x) 
Str 判断是否全部都是空格   a = "     "  x= a.isspace()   print(x) 
Str 判断是否是标题    a = "Hello Word"  x= a.istitle()   print(x) 
Str  将非标题变成标题    a = "Hello word"  x= a.title()   print(x) 
Str 将字符串中的每个元素按照分隔符拼接  

方式一:a = "你好你好"  b = " "  c = b.join(a)  print(c)

方式二:a = "你好你好"  c = " ".join(a)  print(c)

Str 指定占用宽度并将内容居中  20为宽度,"*"为填充字符 a = "hello"  b = a.center(20,"*")   print(b)
Str  指定占用宽度并将内容居左  20为宽度,"*"为填充字符 a = "hello"  b = a.ljust(20,"*")   print(b)
Str  指定占用宽度并将内容居右  20为宽度,"*"为填充字符 a = "hello"  b = a.rjust(20,"*")   print(b)
Str 去除左右空格字符和不可见字符 备注:若括号内写字符,则移除指定字符,从最多开始匹配; a = "   hello   "  b = a.strip()   print(b)
Str  去除左空格字符和不可见字符 备注:若括号内写字符,则移除指定字符,从最多开始匹配; a = "   hello   "  b = a.lstrip()   print(b)
Str  去除右空格字符和不可见字符 备注:若括号内写字符,则移除指定字符,从最多开始匹配; a = "   hello   "  b = a.rstrip()   print(b)
Str  规避敏感信息  

v1 = "不良言论" v2 = "****"    v3 = str.maketrans(v1,v2)

a = "某人发表的不良言论"  b = a.translate(v3)   print(b)

Str 分隔信息,只能分隔三段; 从左到右 备注:指定分隔符显示  a = "abcdffodooodxxx"  b = a.partition("d")   print(b)
Str  分隔信息,只能分隔三段; 从右到左 备注:指定分隔符显示 a = "abcdffodooodxxx"  b = a.rpartition("d")   print(b)
Str  分隔信息,指定分隔段数; 从左到右 备注:指定分隔符不显示 a = "abcdffodooodxxx"  b = a.split("d")   print(b)
Str  分隔信息,指定分隔段数; 从右到左 备注:指定分隔符不显示 a = "abcdffodooodxxx"  b = a.rsplit("d")   print(b)
Str 字符串的替换 代码中的2指只替换前两个匹配字符,如果 不指第三个参数则全替换。 a = "hallohallohallo"  b = a.replace("a","e",2)
Str  只能根据换行符分隔 参数解释:True显示/n,flase不显示换行符 a = "abcdf/nodoo/nodxxx"  b = a.rsplitlines(True)   print(b)
Str  判断以什么内容开头    a = "hello"  b = a.startswith("h")  print(b)
Str  判断以什么内容结尾    a = "hello"  b = a.endswith("o")  print(b)
Str  大小写转换   a = "hello"  b = a.swapcase()   print(b)
Str  获取字符串中的某一个字符   a = "hello"  b = a[3]    print(b)
Str   获取字符串中的某一个字符 0>=范围<3     -1代表最后一位 a = "hello"  b = a[0:3]    print(b)
Str 

Python3中获取的是字符的个数

Python2.7中获取的是字符的字节数

Python3中获取的是字符的个数

Python2.7中获取的是字符的字节数

a = "hello"  b = len(a)    print(b)
Str  一个个输出字符串的方法  a为变量名 

test = "降龙十八掌"

for a  in test:

  print(a)

Str 创建一个范围的数字

0>=范围<100  5为设置的步长(默认步长则是连续的)

注意:Python2.7中一次性创建范围内所有数字;而Python3则做了优化,

只能通过for循环每次创建一个数字;

a = range(0,100,5)

for iterm in a:

  print(iterm)

你可能感兴趣的:(Python字符串和整数常用功能总结)