python_test_0528
python_test_0528
print("pp49_0528") print(str(10000)) ''' 字符串方法 ''' #find str1 = "with a moo-moo" pos = str1.find("moo") print("number"+repr(8)) #print("index is" + str(pos)) #异常信息提示 'str' object is not callable 表示您正在尝试将字符串对象作为函数进行调用,但实际上字符串不是可调用的(即不是函数) #单个纯数字转字符串用str,也可以用repr #str是字符串转换函数,不要拿来当变量用-------------- #print(str(10000)) print(str(10000)) #字符串连接,数字与字符串相加则用repr,repr已合法的python表达式的形式来表示值 str2 = ("index is" + repr(pos)) print("index is " + str(pos)) #p21 title2 = "mononday is first day on xx on" value2 = title2.find("on") print("index is " + str(value2)) value2 = title2.find("kk") print("未找到返回值: " + str(value2)) print("长度: " + str(len(title2))) value2 = title2.find("on",27) print("从索引27位置查找on结果:" + str(value2)) start_index = 1 end_index = 5 value2 = title2.find("on",start_index,end_index) print("设置起始位置的结果:"+ str(value2)) #join方法 seq = ['1', '2'] seq_plus = '_' str3 ="abcdefg" symbol3 = "_" ret3 = [symbol3.join(str3)] #连接方式1 print(ret3) ret5 = [symbol3.join(seq)] #连接方式2 print(ret5) ''' Traceback (most recent call last): File "D:/Users/tangxianwu/PycharmProjects/python_server_test/test0528.py", line 34, in <module> seq_plus.join(seq) ,类型相同才能相连接 TypeError: sequence item 0: expected str instance, int found ''' #所有字母变小写 str5 = "Tangxx" print(str5.lower()) #替换 replace str6 = "this is a test" print(str6.replace("is","good")) #分割split str7 = "aa_bb_cc_dd_ee" print(str7.split('_')) #去除前后空格strip,c# 是trim函数 str8 = " trewq " print(str8.strip()) #指定需要去除的字符空格 str9 ="*** spam * everyone! ***" print(str9.strip(" *!")) #3.4.7 translate替换单个字符 #from string import maketrans #ImportError: cannot import name 'maketrans' from 'string',py3.8不支持,py2.7可以 #table2= maketrans('cs','kz') #print(len(table2)) #table2[97:123] #maketrans('','')[97:123] print("P54")
欢迎讨论,相互学习。
cdtxw@foxmail.com