python-内建函数
-int:将字符串转换为数字
a = "123" print(type(a),a)
b = int(a) print(type(b),b)
num = "0011" v = int(num, base=16) print(v)
- bit_lenght:# 当前数字的二进制,至少用n位表示
age=5 r = age.bit_length() print(r)
字符串:str
1、join #将字符串中的每一个元素按照指定分隔符进行拼接
test = "你是风儿我是沙" print(test) t = ‘ ‘ v = t.join(test) print(v)
你是风儿我是沙
你 是 风 儿 我 是 沙
2、split、rsplit #分割为指定个数
name = " aleX" v=name.split("l") print(v)
[‘ a‘, ‘eX‘]
3、find;从开始往后找,找到第一个之后,获取其位置。找不到:-1
name = " aleX" v=name.find(1) print(v) name = " aleX" v=name.find("e") print(v)
-1 3
3.1、index:查找指定字符。找不到则报错!返回指定字符所在的序列。
test = "alexalex" v = test.index(‘8‘) print(v)
test = "alexalex"
v = test.index(‘a‘)
print(v)
v = test.index(‘8‘) ValueError: substring not found
--0
4、strip、lstrip、rstrip----------移除制定字符串,有限先最多匹配
test = "xadsds" a = test.lstrip(‘xa‘) print(a) b = test.rstrip(‘lexxexa‘) print(b) c = test.strip(‘xas‘) print(c)
dsds
xadsds
dsd
什么都不填的时候,去除左右空白
name = " aleX " v= name.strip() print(v)
aleX
name = " aleX " v= name.strip() print(v) a=name.rstrip() print(a) b=name.lstrip() print(b)
aleX
aleX
aleX
# 去除\t \n
# v = test.lstrip()
# v = test.rstrip()
# v = test.strip()
# print(v)
5、upper、lower变成大小写、isupper、islower,是否全部大小写
test = "Alex" v1 = test.islower() v2 = test.lower() print(v1, v2) v1 = test.isupper() v2 = test.upper() print(v1,v2)
False alex
False ALEX
6、casefold 所有字符变为小写,并且更加牛逼。很多未知的相应变小写
test="Akkis"
v1 = test.casefold() print(v1) v2 = test.lower() print(v2)
akkis@1
akkis@1
7、replace:将指定字符串替换为指定字符串。
test = "alexalexalex" v = test.replace("ex",‘bbb‘) print(v) v = test.replace("ex",‘bbb‘,1) print(v)
albbbalbbbalbbb
albbbalexalex
8、capitalize:首字母变为大写
a = "alex" b = a.capitalize() print(a) print(b)
alex
Alex
原文:http://www.cnblogs.com/laixiaoyun/p/6096077.html