1.变量:命名与使用
#!/usr/bin/env/ python # -*- coding:utf-8 -*- name = ‘liQM‘
2.字符串:字符串一旦创建,不可修改,修改或者拼接都会创建新的字符串。
print(‘hello world!‘)
def capitalize(self, *args, **kwargs)
1 例子: 2 name = ‘jack‘ 3 test = name.capitalize() 4 print(test) 5 6 输出: 7 Jack
def lower(self)
1 例子: 2 name = ‘jACk‘ 3 test = name.capitalize() 4 print(test) 5 6 输出: 7 jack
def center(self, *args, **kwargs)
1 例子: 2 name = ‘jack‘ 3 test = name.center(20,‘*‘) 4 print(test) 5 6 输出: 7 ********jack********
count:返回子字符在字符串中出现的次数,可选位置范围。
def count(self, sub, start=None, end=None)
1 例子: 2 name = ‘jackjack‘ 3 test = name.count(‘a‘,2,6) 4 print(test) 5 6 输出: 7 1
def endswith(self, suffix, start=None, end=None) 结尾 def startswith(self, suffix, start=None, end=None) 开头
1 例子: 2 name1 = ‘jackjack‘ 3 name2 = ‘jackjack‘ 4 test1 = name1.endswith(‘k‘) 5 test2 = name2.startswith(‘a‘) 6 print(test1) 7 print(test2) 8 9 输出: 10 True 11 False
def find(self, sub, start=None, end=None)
1 例子: 2 name = ‘jackjack‘ 3 test = name.find(‘k‘,5,8) 4 print(test) 5 6 输出: 7 7
def format(self, *args, **kwargs)
1 例子: 2 test = ‘i am {name}, age {a}‘ #‘i am {0}, age {1} 3 v = test.format(name=‘alex‘,a = 19) #test.format(‘alex‘,a = 19) 4 print(v) 5 6 输出: 7 i am alex, age 19
1 def format_map(): 2 例子: 3 test = ‘i am {name}, age {a}‘ 4 v = test.format_map({‘name‘:‘jcak‘, ‘a‘:10}) 5 print(v) 6 7 输出: 8 i am jcak, age 10
def isalnum(self, *args, **kwargs)
1 例如: 2 name = ‘jackjack123‘ 3 test = name.isalnum() 4 print(test) 5 6 输出: 7 True
def expandtabs(self, tabsize=None)
1 例如: 2 name = ‘username\tpassword\temail\nJack\t123\tjack@163.com\nMick\t456\tmick@163.com\n‘ 3 test = name.expandtabs(10) 4 print(test) 5 6 输出: 7 username password email 8 Jack 123 jack@163.com 9 Mick 456 mick@163.com
def join(self, ab=None, pq=None, rs=None)
1 例如: 2 test = ‘中华人民共和国万岁!‘ 3 t = ‘_‘ 4 v = t.join(test) #v = ‘_‘.join(test) 5 print(v) 6 7 输出: 8 中_华_人_民_共_和_国_万_岁_!
def strip(self, chars=None)
1 例子: 2 test = ‘ jack ‘ 3 v = test.strip() 4 print(v) 5 6 输出: 7 jack (左右两边无空格)
def maketrans(self, *args, **kwargs) def translate(self, *args, **kwargs)
1 例子: 2 ‘‘‘test = ‘abcdefg‘ 3 v = test.translate(str.maketrans(‘acf‘,‘123‘)) 4 ‘‘‘ 5 #上面的代码输出结果与下面的一样 6 test = ‘abcdefg‘ 7 v1 = str.maketrans(‘acf‘,‘123‘) 8 v = test.translate(v1) 9 print(v) 10 11 输出: 12 1b2de3g
def partition(self, *args, **kwargs) def rpartition(self, *args, **kwargs)
1 例子: 2 test = ‘sbkfjwnkls‘ 3 v1 = test.partition(‘k‘) 4 v2 = test.rpartition(‘k‘) 5 print(v1,v2) 6 7 输出: 8 (‘sb‘, ‘k‘, ‘fjwnkls‘) (‘sbkfjwn‘, ‘k‘, ‘ls‘)
def split(self, sep=None, maxsplit=None)
1 例子: 2 test = ‘sbkfjkwnkls‘ 3 v1 = test.split(‘k‘,2) 4 v2 = test.rsplit(‘k‘,2) 5 print(v1,v2) 6 7 输出: 8 [‘sb‘, ‘fj‘, ‘wnkls‘] [‘sbkfj‘, ‘wn‘, ‘ls‘]
def swapcase(self, *args, **kwargs)
1 例子: 2 test = ‘jaCk‘ 3 v = test.swapcase() 4 print(v) 5 6 输出: 7 JAcK
def replace(self, old, new, count=None)
1 例子: 2 test = ‘ABCDABCDABCD‘ 3 v = test.replace(‘AB‘,‘aaa‘,2) 4 print(v) 5 6 输出: 7 aaaCDaaaCDABCD
1 l例子: 2 test = ‘abcdefg‘ 3 v = test[3] 4 print(v) 5 6 输出: 7 d
1 例子: 2 test = ‘abcdefg‘ 3 v = test[1:3] 4 print(v) 5 6 输出: 7 bc
1 例子: 2 test = ‘abcdefg‘ 3 v = len(test) 4 print(v) 5 6 输出: 7 7
1 例子: 2 test = ‘你好啊‘ 3 index = 0 4 while index < len(test): 5 v = test[index] 6 index += 1 7 print(v) 8 9 输出: 10 你 11 好 12 啊
1 例子: 2 test = ‘你好啊‘ 3 for v in test: 4 print(v) 5 6 输出: 7 你 8 好 9 啊
1 例子: 2 test = range(0,6,2) 3 for v in test: 4 print(v) 5 6 输出: 7 0 8 2 9 4
汇总例子:
1 例子: 2 test =input(‘请输入:‘) 3 for v in range(len(test)): 4 print(v,test[v]) 5 6 输出: 7 请输入:你好啊 8 0 你 9 1 好 10 2 啊
def bit_length(self)
1 >>> bin(37) 2 ‘0b100101‘ 3 >>> (37).bit_length() 4 6
......
原文:https://www.cnblogs.com/liqiongming/p/9991824.html