Python基础备份记录(二)

#!/usr/localbin/python3

# -*- coding: utf-8 -*-

# https://www.cnblogs.com/wupeiqi/articles/5444685.html

# 结果是值的分成下面两类

# 算数运算符 赋值运算符

# ========================

# 结果是布尔值的分成下面三类

# 比较运算符 逻辑运算符 成员运算符

# 基本数据类型

## 数字

## 字符串

## 布尔值

# python3 只有int 整形之说

# int str list tuple dict bool

# int --->  数字 所有的功能,都放在int里

"""

字符串转换为数字类型

进制转换

num ="0011"

v = int(num, base=16)

print(v)

age = 5

r = age.bit_length()

print(r)

bit_length 当前数字的二进制至少用几位来表示

所有变小写,只是casefold更牛逼,他有很多的未知对应关系

v = test.center(20)

print(v)

表示有20个位置并将内容居中,后面没有就是默认是空白

v = test.center(20,"*") 后面有* 就是使用*填充

endswith startswith 开头和结束

encode decode  编码和解码

find 从前往后找

format

test = 'i am {name}'

print(test)

v = test.format(name='root')

print(v)

index 索引的位置

isalnum 判断是不是只包含字母和数字 ,都是数字也可以,都是字母也可以

isupper

"""

# capitalize 是将首字母变大

n = 'root'

print(n)

v = n.capitalize()

print(v)

print('===========capitalize==========')

# center:内容居中,width:总长度, fillchar:空白处需要填充的内容,不填默认是空白

#  def center(self, width, fillchar=None):

n = 'cntf'

print(n)

v = n.center(10)

v = n.center(30,"-")

print(v)

print('==========center===========')

# count 子序列个数,判断有多少个字符

# 例如:str.count(sub, start=0,end=len(string))

# 函数:def count(self, sub, start=None, end=None):

# sub -- 搜索的子字符串

# start -- 字符串开始搜索的位置,默认为第一个字符串,第一个字符索引值为0

# end -- 字符串中结束搜索的位置,

n = 'this is string test cntf ... hehe'

print(n)

v = n.count('he', 0, 50)

print(v)

print('==========count===========')

# endswith 是否以....结束....

# def endswith(self, suffix, start=None, end=None):

n = 'this is cntf test python3'

print(n)

# v = n.endswith('on3')

v = n.endswith('tf', 0, 12)

print(v)

print('==========endswith===========')

# expandtabs 将tab转换成空格,默认一个tab转换成8个空格

# def expandtabs(self, tabsize=None):

# 默认情况下tabsize不写,就是将tab转换成8个空格,如果写成2,就是转换成2个空格

n = 'zhege shi ceshi zhidaobu cntf'

print(n)

v = n.expandtabs(2)

print(v)

print('==========expandtabs===========')

# find  寻找子序列位置,如果没有找到,就返回-1

# def find(self, sub, start=None, end=None):

n = 'ceshi find zhege danci word cntf'

print(n)

# v = n.find('danci')

v = n.find('fuck')

print(v)

print('==========find===========')

# 上述显示的结果是17,表示从第一个字符开始从0开始数,第17个字符是符合要求的

# 上述如果没有找到fuck那么就是显示-1

# format 字符串格式化,动态参数

# def format(*args, **kwargs): # known special case of str.format

# 通过位置来填充字符串

n = 'hello {0} I will fucking the {1}'

print(n)

v = n.format('objk', 'world')

print(v)

print('==========format-位置测试===========')

n1 = 'hello {} I will fucking the {}'

print(n1)

v1 = n1.format('ojbk','world')

print(v1)

print('==========format-不写位置测试===========')

n2 = 'hello {0} I will fucking the {1} ... and now this is a programe language--- {1}'

print(n2)

v2 = n2.format('ojbk', 'world')

print(v2)

print('==========format-同一个参数可以填充多次===========')

# 同一个参数可以填充多次,这个是format的先进地方

# 通过key来填充

jbkb = 'world'

name = 'python3'

print('hello, {jbkb}, I will fuck {name}')

# 开始format

print('hello, {jbkb}, I will fuck {name}'.format(jbkb = jbkb, name = name))

print('==========format-通过key来填充===========')

# 通过列表填充

list = ['world', 'python3']

print('hello {names[0]}  I will fuck {names[1]}')

# 开始format

print('hello {names[0]}  I will fuck {names[1]}'.format(names=list))

print('==========format-通过列表来填充===========')

# 通过字典填充

dict = {'obj':'world', 'name':'python3'}

print(dict)

print('hello {names[obj]} I will fucking {names[name]}'.format(names=dict))

print('==========format-通过字典来填充===========')

print('***********format-已经测试完毕************')

# index 子序列位置,如果没有找到,就报错

# def index(self, sub, start=None, end=None):

n = 'that if fuck the world'

print(n)

v = n.index('fuck')

print(v)

print('==========index测试完毕===========')

# 在字符串中查找fuck的起始位置,这里执行结果是8,表示从左到右从0开始数起第8个位置

# isalnum 是否是字母和数字,或者都是字母,或者都是数字

# def isalnum(self): 

n1 = 'cntf888'

n2 = 'cntf'

n3 = '9988899223'

n4 = 'cntf_fuck998'

print(n1)

print(n2)

print(n3)

# 开始判断是否是数字字母或者其中一个是的

v1 = n1.isalnum()

print(v1)

v2 = n2.isalnum()

print(v2)

v3 = n3.isalnum()

print(v3)

v4 = n4.isalnum()

print(v4)

print('==========isalnum测试完毕===========')

# isalpha 是否是字母

# def isalpha(self):

n1 = 'cntf'

n2 = '888666'

print(n1)

v1 = n1.isalpha()

print(v1)

v2 = n2.isalpha()

print(v2)

print('==========isalpha测试完毕===========')

# isdigit 是否是数字

# def isdigit(self):

n1 = 'cntf'

n2 = '888999'

print(n1)

v1 = n1.isdigit()

print(v1)

v2 = n2.isdigit()

print(v2)

print('==========isdigit测试完毕===========')

# islower 是否是小写

# def islower(self):

n1 = 'cnft'

n2 = 'CNTf'

print(n1)

print(n2)

v1 = n1.islower()

print(v1)

v2 = n2.islower()

print(v2)

print('==========islower测试完毕===========')

# isspace 是否含有空格

# def isspace(self):

n1 = 'cntf'

n2 = '  '

print(n1)

print(n2)

v1 = n1.isspace()

print(v1)

v2 = n2.isspace()

print(v2)

print('==========isspace测试完毕===========')

# istitle 判断字符串中所有的单词拼写首字母是否为大写,且其他字母为小写

# def istitle(self):

n1 = 'This Is String Da Xue'

n2 = 'This is string test shifou shi daxie'

print(n1)

print(n2)

v1 = n1.istitle()

print(v1)

v2 = n2.istitle()

print(v2)

print('==========istitle测试完毕===========')

# isupper 判断字符串中所有的字母是否是大写

# def isupper(self):

n1 = 'THIS IS TEST ALL ZIMU SHIFOU SHI DAXUE'

n2 = 'THIS is all zimu no daxue'

print(n1)

print(n2)

v1 = n1.isupper()

print(v1)

v2 = n2.isupper()

print(v2)

print('==========isupper测试完毕===========')

# join 将序列中的元素以指定的字符连接生成一个新的字符串

n1 = 'fuck'

n2 = '---'

print(n1.join(n2))

str = "-"

seq = ("a", "b", "c")

print(str.join(seq))

print('==========join测试完毕===========')

# ljust 内容左对齐,右侧填充

# def ljust(self, width, fillchar=None):

n1 = 'cntf that is fucking '

print(n1)

v1 = n1.ljust(30, '2')

print(v1)

print('==========ljust测试完毕===========')

# lower 变成小写

# def lower(self):

n = 'cntFFUCKingheHe'

print(n)

v = n.lower()

print(v)

print('==========lower测试完毕===========')

# lstrip 移除最侧空白

# def lstrip(self, chars=None):

n = '      d shiting cntf FUK HEIHEI'

print(n)

v = n.lstrip()

print(v)

print('==========lstrip测试完毕===========')

# partition 分割,前,中,后三部分

# def partition(self, sep):

# 根据指定的分隔符将字符串进行分割

n = 'www.baidu.com'

print(n)

v = n.partition('bai')

print(v)

print('==========partition测试完毕===========')

# 执行的结果就是根据前,中,后来看

# replace 替换

# def replace(self, old, new, count=None):

n = 'www.baidu.com.net'

print(n)

v = n.replace('www','shit')

print(v)

v1 = n.replace('.','*')

v2 = n.replace('.','*',2)

print(v1)

print(v2)

print('==========replace测试完毕===========')

# old 是指将要被替换的字符串

# new 是指被替换后的新字符串

# max 可选字符串,表示替换不超过多少次

# rfind 返回字符串最后一次出现的位置,如果没有匹配则返回-1

# def rfind(self, sub, start=None, end=None):

n1 = 'this is ceshi rfingd fucking cntf'

n2 = 'cn'

print(n1)

print(n2)

v1 = n1.rfind('is', 0, 5)

v4 = n1.rfind('is', 5, 0)

v2 = n1.rfind('is')

v3 = n1.find('is')

print(v1)

print(v2)

print(v3)

print(v4)

print('==========rfind测试完毕===========')

# find 是从字符串左边开始查询子字符串匹配到的第一个索引

# rfind 默认情况下是从字符串右边开始查询子字符串匹配到的第一个索引

# 当然如果rfind已经指定了开始和结束的位置那么就是根据指定的位置开始从左至右

# rindex 子字符串最后一次出现在字符串中的索引位置,只不过如果子字符串不在字符串中会报一个异常

# def rindex(self, sub, start=None, end=None): 

n1 = 'this is ceshi rfingd fucking cntf'

print(n1)

v1 = n1.rindex('is')

# v2 = n1.rindex('is', 10)

print(v1)

# print(v2)

print('==========rindex测试完毕===========')

# 默认是从左向右开始查找匹配,如果指定了起始位置那么就从起始位置开始查找匹配

# rjust 返回一个原字符串右对齐,并使用空格填充

# def rjust(self, width, fillchar=None): 

n1 = 'this is fucking cntf'

print(n1)

v1 = n1.rjust(30)

v2 = n1.rjust(30, '*')

print(v1)

print(v2)

print('==========rjust测试完毕===========')

# rpartition 从右向左边开始以前,中,后分割

# def rpartition(self, sep): 

n = 'www.baidu.com.net'

print(n)

v = n.rpartition("du")

print(v)

print('==========rpartition测试完毕===========')

# rsplit 从左到右开始指定分隔符对字符串进行分割并返回一个列表,默认分隔符为所有的空字符,包括空格,换行(\n),制表符(\t)

# def rsplit(self, sep=None, maxsplit=None): 

# sep 可选参数,指定分隔符,默认为所有的空字符,包括换行符和制表符

# count 可选参数,分割次数,默认为分隔符在字符串中出现的总次数

n1 = 'this is ceshi rsplit fucking cntf'

print(n1)

v1 = n1.rsplit()

print(v1)

v2 = n1.rsplit('i')

print(v2)

v3 = n1.rsplit('i', 1)

print(v3)

print('==========rsplit测试完毕===========')

# rstrip 返回删除字符串末尾指定的字符

# def rstrip(self, chars=None): 

n1 = " ***********this fucking cntf************"

n2 = ' ***********this fucking cntf************  '

print(n1)

v1 = n1.rstrip()

print(v1)

v2 = n2.rstrip('*')

print(v2)

print('==========rstrip测试完毕===========')

# split 分割, maxsplit 最多分割几次

# def split(self, sep=None, maxsplit=None): 

n1 = 'this is ceshi fenge fucking cntf'

print(n1)

v1 = n1.split()

print(v1)

v2 = n1.split('e')

print(v2)

v3 = n1.split('e', 1)

print(v3)

# 真的字符串进行分割,默认是以空白符,空格进行分割

# sep 是指定以什么作为分割符

# maxsplit 指定最大分割几次

print('==========split测试完毕===========')

# splitlines 根据换行分割

# def splitlines(self, keepends=False): 

n1 = 'this is ceshi fenge fucking cntf'

print(n1)

v1 = n1.splitlines()

print(v1)

print('==========splitlines测试完毕===========')

# startswith 检查字符串是否是以指定的子字符开头,如果是返回True,如果不是返回False

# 如果参数beg和end指定值,则在指定范围内检查

# def startswith(self, prefix, start=None, end=None): 

n = 'this is test start swith hehe example wowow  ok ...!!!'

print(n)

v = n.startswith('this')

print(v)

v1 = n.startswith('is')

print(v1)

v2 = n.startswith('test', 8)

print(v2)

print('==========startswith测试完毕===========')

# strip 移除两段空白,或者指定的字符串

# def strip(self, chars=None):

n1 = '00000000009988cntf-shiting889900000000000000'

print(n1)

v1 = n1.strip()

print(v1)

# 默认不指定就是空白符

v2 = n1.strip('0')

print(v2)

n2 = '        shiting and fucking cntf          '

print(n2)

v4 = n2.strip()

print(v4)

print('==========strip测试完毕===========')

# swapcase 大写变小写,小写变大写

# def swapcase(self):

n1 = 'CNTF'

n2 = 'cntf'

n3 = 'CNtf'

print(n1)

print(n2)

print(n3)

v1 = n1.swapcase()

print(v1)

v2 = n2.swapcase()

print(v2)

v3 = n3.swapcase()

print(v3)

print('==========swapcase测试完毕===========')

# title 所有单词都是以大写开头

# def title(self):

n = 'this is test the titles cntf'

print(n) 

v = n.title()

print(v)

print('==========title测试完毕===========')

# translate 需要转换的字符

# 转换,需要先做一个对应表,最后一个表示删除字符集合

# def translate(self, table, deletechars=None): 

# from string import maketrans  # Required to call maketrans function.

# intab = "aeiou"

# outtab = "12345"

# trantab = maketrans(intab, outtab)

# str = "this is string example....wow!!!";

# print str.translate(trantab, 'xm');

# print('==========translate测试完毕===========')

# upper 将小写字母变成大写

# def upper(self):

n = 'fucking'

print(n)

v = n.upper()

print(v)

print('==========upper测试完毕===========')

# zfill 方法返回指定长度的字符串,原字符串右对齐,前面填充0

# def zfill(self, width):

n1 = 'shit'

print(n1)

v1 = n1.zfill(60)

print(v1)

print('==========zfill测试完毕===========')

# expandtabs 根据制表符进行匹配

test = '1234567\t89'

print(test)

v = test.expandtabs(6)

print(v,len(v))

# expandtabs(6)表示针对字符每6个字符为一组,如果找到每组中含有制表符\t

# 那么算出这个有制表符的组中除了对于的字符之外的位置用空格填充

# 实际用途中可以制作表格

# isdecimal isdigit

# 判断是否为数字,isdigit牛逼点 isnumeric更牛逼

n = "⑥"

# v1 = n.isdecimal()

v2 = n.isdigit()

v3 = n.isnumeric()

print(v1,v2,v3)

# Python3正常执行

# isidentifier()

# a = "def"

# v = a.isidentifier()

# print(v)

# isprintable 是否可以打印 例如\n \t 是不可以打印的

# 需要记住经常使用的 join split find strip upper lower replace

test = input(">>>")

for item in range(0, len(test)):

print(item, test[item])

你可能感兴趣的:(Python基础备份记录(二))