ord(‘h’)----> 104
chr(100) ?----> 'd'
str1[::-1]:反序
str1.capitalize():首字母大寫,其余小寫
str1.title():
str1.center(width):居中,空格填充
str1.count(substr,0,n)
str.decode()
str.encode()
str.endwith()
str.expandtabs()
str.find():返回第一個目標字符出現的位置,查找不到返回‘-1’
str.index():查找不到返回異常
str.isalnum():是否全是字母或數字
str.isalpha():是否全是字母
str.isdecimal():
str.isdigit():是否全是數字
str.islower():
str.isnumeric():
str.isspace():是否全是空白字符,并至少有一個字符
str.istitle()
大小寫
str.isupper()
str.swapcase():大小寫互換
str.lower()
字符串連接
str.join(seq):把seq序列用str連接起來
>>> print "#".join("bcdefg")
b#c#d#e#f#g
字符對齊
str.ljust(width):向左對齊,右邊補空格
str.rjust(width):向右對齊
str.zfill(width):右對齊,補0
去空格
str.strip()
str.lstrip()
字符串替換
str.replace(old,new)
字符串分割
str.split()
str.splitlinres([keepends]):按行分割符分為一個list
首尾判斷
str.startswith(substr):是否以substr開頭
str.endswith(substr):是否以substr結尾
類型轉換
import string
str.atoi(s1,10)
str.atoi(s1,16)
str.atol(s)
str.atof(s,base)
轉換
replist = string.maketrans(s,r) ?s到r的轉換表
s.translate(replist)
string.capwords(s,seq=None):以seq分割后首字母大寫