首页 > 技术文章 > int,str内置方法

amiee-785563694 2017-04-25 08:31 原文

一 int

整数,int

x=123243;y=2
#x.bit_length,显示二进制的长度 print(x.bit_length())
#conjugate 共轭复数 #denominator #from_bytes #imag #numerator #real #to_bytes

  

二 str

'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'

(字符串特殊操作)

3.7 capitalize(首字母大写)

Str capitalize

>>> a1="hello"

>>> ret1=a1.capitalize()

>>> print ret1

Hello

 

3.8 center(字符串居中)

字符串居中,center括号内,第一个字段为长度,第二个字段单引号内指定两边的填充符

>>> a2="hello"

>>> ret2=a2.center(20,'*')     #20表示一个20个字符的字符串;*表示两边不够,用*填充

>>> print ret2

*******hello********

 

3.9 count(子序列计数)

count子序列的个数,统计count后括号内字符串出现的次数,后面不加参数表示查询全部。

变量内每个字符串称为子序列,可以指定长度查询,0,表示从第一个开始查询,4表示长度,从第一个开始到第四个,空格也算

>>> a3="this is world"

>>> ret3=a3.count("is",0,4)

>>> print(ret3)

1

 

 

3.10 endswith(是否是指定字符串结尾)

查看是否为指定字符结尾,是返回True,不是返回False,同样可以指定子序列

>>> temp1="hello"

>>> print(temp1.endswith("e"))  #默认是查看最后一位

False

>>> print(temp1.endswith("e",0,2))  #查看第2段是否是e

True

>>> print(temp1.endswith("lo"))

True

 

3.11 expandtabs(将tab换成空格)

Expandtabs将tab换成空格,一个tab转换为8个空格,也可以指定转换为多少空格

>>> content="hello\t999"     # \t表示tab键

>>> print(content)

hello 999

>>> print(content.expandtabs())    #默认把tab键转换为8个空格

hello   999

>>> print(content.expandtabs(20))   #把tab键转换为20个空格

hello               999

3.12 find(寻找子序列的位置)

Find 寻找子序列的位置,子序列是自定义的,可以自定义一个或多个字符串,空格也算

>>> s="tom hello"

>>> print(s.find("l"))

6

>>> print(s.find("ll"))

6

3.13 format(字符串的格式化)

#{0}为占位符

>>> s1="hello {0},age {1}"     #{0}第一个占位符、{1}第二个占位符

>>> print(s1)

hello {0},age {1}

>>> new1=s1.format('tom',19)    #tom赋值给第一个占位符,19赋值给第二个占位符

>>> print(new1)

hello tom,age 19

3.14 index(寻找子序列)

Index寻找子序列,没找到会报错,类似于find,使用find就可以

3.15 isalnum(判断字符是否是字母和数字)

Isalnunm判断字符是否是字母和数字,是返回True,不是返回False

>>> s2="tom123hello"

>>> print(s2.isalnum())

True

>>> s2=" \;'"

>>> print(s2.isalnum())

False

3.16 isalpha(判断字符是否全部是字母)

Isalpha判断字符是否全部是字母

>>> s3="hello"

>>> print(s3.isalpha())

True

>>> s3="hello123"

>>> print(s3.isalpha())

False

3.17 isdigit(检测是否都是数字)

isdigit检测是否都是数字

>>> s4="1234"

>>> print(s4.isdigit())

True

 

3.18 islower(检查是否全部是小写)

islower检查是否全部是小写

>>> s5="hello"

>>> print(s5.islower())

True

 

3.19 isspace(判断是否是空格)

isspace判断是否是空格

>>> s6="   "

>>> print(s6.isspace())

True

 

3.20 istitle(判断是否是标题)

Istitle判断是否是标题,每个单词的首字母为大写

>>> s7="The School"

>>> print(s7.istitle())

True

 

3.21 isupper(判断是否全部是大写)

isupper判断是否全部是大写

>>> s8="HELLO"

>>> print(s8.isupper())

True

 

 

3.22 join(连接序列中的元素)

Join可以用指定连接符将字符串连接起来,可以不加,不加时连接符为空。

注意:需要连接的序列的元素必须是字符串

>>> li=["tom","eric"]

>>> yz=("joy","hello")

>>> s9="_".join(li)

>>> print(s9)

tom_eric

>>> s10="@".join(yz)

>>> print(s10)

joy@hello

 

 

3.23 ljust(内容左对齐)

Ljust内容左对齐,右侧填充,也可以指定宽度

>>> s11="hello"

>>> print(s11.ljust(20,"*"))

hello***************

 

 

3.24 lower(把字符串变小写)

lower把字符串变小写

>>> s12="HELLO"

>>> print(s12.lower())

hello

 

 

3.25 lstrip(移除左边的空格)

lstrip移除左边的空格

>>> s3="   hello"

>>> print(s3.lstrip())

hello

 

3.26 rstrip(移除右边的空格)

rstrip移除右边的空格

>>> s4="hello    "

>>> print(s4.rstrip())

hello

 

3.27 strip(左右两边的空格全部去掉)

strip左右两边的空格全部去掉,换行符

>>> s15="  hello   "

>>> print(s15.strip())

hello

s='*********let\'s go*********'
print(s.strip("*"))

 

 

3.28 partition(指定分割符)

partition指定的分隔符,跟split有点像,不过split不会包含wen

>>> s16="hello wen python"

>>> fen=s16.partition("wen")

>>> print(fen)

('hello ', 'wen', ' python')

 

3.29 replace(替换指定字符)

replace替换指定字符,可以指定范围

Replace方法返回某字符串的所有匹配项均被替换之后得到的字符串。类似于文本编辑器的”查找并替换”

>>> s17="hello wen python"

>>> temp17=s17.replace("wen","wenyanjie")

>>> print(temp17)

hello wenyanjie python

 

3.30 rfind(从右到左寻找)

 

3.31 rjust(右对齐)

 

3.32 split(将字符串分割成序列)

split, 指定分隔符分割字符串,将分隔符去除,显示其他部分

>>> s18="hellohello"

>>> print(s18.split())

['hellohello']

>>> print(s18.split("e"))

['h', 'lloh', 'llo']

 

3.33 rsplit(从右到左分割)

Rsplit 从右到左分割

Splitline-------默认对\n进行分割

3.34 startswith(是否是指定字符串开头)

startswith判断是否是某个字符或字符串开头,相对于endswith

3.35 swapcase(大小写互换)

3.36 title(字符串变换成标题)

title变换成标题,即把字符串变为首字母大写

3.37 upper(全变大写)

3.38 format(格式化字符串函数)

S=”abcd {name} efgh”.format(name=”egon”)

3.39 split(将字符串以特殊字符分隔成列表)

S=”alex is a sb”

s.split(“ ”)

3.40 startswith(以什么字母开头)

推荐阅读