要一直走下去

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

一、整型(int)

# int对象初始化
x = 2
y = int(3)
n = int("A3",12)
# 运算符(+、-、*、/、//、%、**)
'''
相关的函数
'''
abs(x)       #求绝对值
divmod(x,y)  #求x/y的商和余数,返回元祖
pow(x,y)     #求x的y次方
round(x,n)   #求x四舍五入的值(精确到小数点后n位)
bin(x)       #x的二进制表示
hex(x)       #x的十六进制表示
oct(x)       #x的八进制表示
int(x)       #初始化或者x类型强制转换为int类型
int(s,base)  #初始化或者强制转换

二、布尔(bool)略

三、浮点

'''
三种浮点类型:float、complex、decimal.Decimal
说明:浮点数会丢失很小的精度,需要完全准确要用Decimal类型,但是计算速度比float慢
'''
round(3.1415926,2)      # 3.14
math.ceil(3.1415926,2)  # 3.15
math.floor(3.1415926,2) # 3.14
import decimal
'''
decimal与float之间转换会丢失精度
'''
x = decimal.Decimal(3)
y = decimal.Decimal("4.5552229")
print(x+y)

四、字符串

'''
字符串切片,同样适用于list、tuple
'''
str = "abcdefg"
#取"abcde",两种方式
s = str[:5]
s = str[:-2]
#取"cde",两种方式
s = str[2:5]
s = str[-5:-2]
#反转字符串 "gfedcba"
s = str[::-1]

 字符串常用方法:

###########################一、字符串替换####################
s29 = "**tk**tk***tk**".replace("t","f",2)  # **fk**fk***tk** 前两个t换成f,不写则替换所有
###########################二、格式化输出####################
s11 = "{0}{1}{2}".format("183942","@","qq.com") # 183942@qq.com
###########################三、序列合并字符串####################
s23 = "-".join(("1","2","3"))        # 1-2-3
###########################四、字符串拆分####################
s28 = "*F**F****F*".partition("F") # ('*', 'F', '**F****F*')用"F"断开,取右边部分
s37 = "*F**F****F*".rpartition("F") # ('*F**F****', 'F', '*')用"F"断开,取左边部分
s30 = "6+7+8+9".split("+",2)  # ['6', '7', '8+9'],用+号从左边开始分隔2次,默认为""分隔最大次
s31 = "6+7+8+9".rsplit("+",2)  # ['6+7', '8', '9'],用+号从右边开始分隔2次,默认为""分隔最大次
s32 = "G\nGW\nGWG\n".splitlines() # ['G', 'GW', 'GWG'], 写了True:['G\n', 'GW\n', 'GWG\n']
########################五、首尾去空格或指定字符#################
s34 = "13Miles6".strip("126")    # 3Miles 首尾去掉"126"的每个字符,不写默认去空白
s35 = "13Miles6".lstrip("126")    # 3Miles6
s36 = "13Miles6".rstrip("126")    # 13Miles
############################六、统计字符个数######################
s3 = "adom_of_oppo".count("o",1,6) # 2 ,"adom_of_oppo"[1:6]中"o"的个数
##############################七、编码#############################
s4 = "你好".encode("gbk")      # b'\xc4\xe3\xba\xc3'
############################八、查找字符下标######################
s7 = "foundationable".find("n",2,11) # 3 "undationa"中最左边"n"的下标,没有返回-1
s8 = "foundationable".rfind("n",2,11) # 9 "undationa"中最右边"n"的下标,没有返回-1
s9 = "foundationable".index("n",2,11) # 3 同find,没有抛出异常
s10 = "foundationable".rindex("n",2,11) # 9 同rfind,没有抛出异常

 字符填充

>>> s = 'The sword of truth'
>>> "{0}".format(s)
'The sword of truth'
>>> "{0:25}".format(s)
'The sword of truth       '
>>> "{0:>25}".format(s)
'       The sword of truth'
>>> "{0:^25}".format(s)
'   The sword of truth    '
>>> "{0:-^25}".format(s)
'---The sword of truth----'
>>> "{0:.>25}".format(s)
'.......The sword of truth'
>>> maxwidth = 10
>>> "{0:.{1}}".format('love',maxwidth)
'love'
>>> "{0:{1}}".format('love',maxwidth)
'love      '
View Code

 

字符串其他方法:

######################填充########################
s2 = "love".center(20, "*")      # ********love********
s24 = "Fu".ljust(8,"*")         # Fu******
s25 = "ck".rjust(8,"*")         # ******ck
s40 = "**".zfill(10)   # 00000000**  长度<10在左边添0补全
#####################判断开头结尾字符##############
s33 = "Miles_Guo".startswith("M",2,6) # False  "Miles_Guo"[2:6]是否以'M'开头
s5 = "miles_guo".endswith("_",2,6) # True "miles_guo"[2,6]是否以"_"结尾
######################制表符#######################
s6 = "f\tf\tf\t".expandtabs(1)     # 制表符用1个空格替换,很少用
#########################正则判断######################
s12 = "abc123".isalnum()   # (非空&数字|字母)True
s13 = "abcd".isalpha()      # (非空&字母) True
s14 = u"120990".isdecimal()     # True 对于Unicode,只包含十进制字符
s15 = "120990".isdigit()      # True 字符串只包括ASCII字符
s16 = "&_3".isidentifier()   # False 判断是否为标识符
s17 = "sd**^*^#@().d".islower  # True 全部字母都为小写为真
s18 = u"3714".isnumeric()     # True 都为Unicode的数字
s19 = "2 \n".isprintable()      # False 是否每个字符都可打印
s20 = "\t".isspace()           # True 判断是否都为空白字符
s21 = "Tom Cat".istitle()      # True 判断每个单词首字母大写
s22 = "GFW&(*&(&^*^*&".isupper() # True 判断每个字母都大写
###########################密码本解密######################
code_signal = "".maketrans("12345","天王盖地虎")  # 密码本,复杂的用codec模块
plain_text = "55322".translate(code_signal)     # "虎虎盖王王",复杂的用codec模块
###########################大小写转换#######################
s26 = "Fu**oFF".lower()         # fu**off
s27 = "fu**off".upper()         # FU**OFF
s1 = "jar of love".capitalize()  # Jar of love
s37 = "FUCK off".swapcase()       # fuck OFF 大小写互换
s38 = "everything is begginning...".title()  # Everything Is Begginning... 单词首字母大写

 

posted on 2018-07-06 09:40  要一直走下去  阅读(228)  评论(0编辑  收藏  举报