1。已知一个字符串为helloworldyoyo,如何得到一个队列〔hello,world,yoyo〕?使用split函数,分割字符串,并且将数据转换成列表类型testhelloworldyoyoprint(test。split())结果〔hello,world,yoyo〕2。有个列表〔hello,world,yoyo〕,如何把列表里面的字符串联起来,得到字符串helloworldyoyo?使用join函数将数据转换成字符串:test〔hello,world,yoyo〕print(。join(test))结果:helloworldyoyo 如果不依赖python提供的join方法,还可以通过for循环,然后将字符串拼接,但是在用连接字符串时,结果会生成新的对象,使用join时结果只是将原列表中的元素拼接起来,所以join效率比较高。for循环拼接如下:test〔hello,world,yoyo〕定义一个空字符串j通过for循环打印出列表中的数据foriintest:jji因为通过上面的字符串拼接,得到的数据是helloworldyoyo,前面会多一个下划线,所以把这个下划线去掉print(j。lstrip())3。把字符串s中的每个空格替换成20,输入:sWearehappy。,输出:We20are20happy。。使用replace函数,替换字符换即可:sWearehappy。print(s。replace(,20))12结果:We20are20happy。4。Python如何打印99乘法表?for循环打印:foriinrange(1,10):forjinrange(1,i1):print({}x{}{}。format(j,i,ij),end)print()while循环实现:i1whilei9:j1whileji:print(dd2d(i,j,ij),end)d:整数的占位符,2代表靠左对齐,两个占位符j1print()i1结果1x111x222x241x332x363x391x442x483x4124x4161x552x5103x5154x5205x5251x662x6123x6184x6245x6306x6361x772x7143x7214x7285x7356x7427x7491x882x8163x8244x8325x8406x8487x8568x8641x992x9183x9274x9365x9456x9547x9638x9729x9815。从下标0开始索引,找出单词welcome在字符串Hello,welcometomyworld。中出现的位置,找不到返回1。deftest():messageHello,welcometomyworld。worldwelcomeifworldinmessage:returnmessage。find(world)else:return1print(test())结果:76。统计字符串Hello,welcometomyworld。中字母w出现的次数。deftest():messageHello,welcometomyworld。计数num0for循环messageforiinmessage:判断如果‘w’字符串在message中,则num1ifwini:num1returnnumprint(test())结果27。输入一个字符串str,输出第m个只出现过n次的字符,如在字符串gbgkkdehh中,找出第2个只出现1次的字符,输出结果:ddeftest(strtest,num,counts)::paramstrtest:字符串:paramnum:字符串出现的次数:paramcount:字符串第几次出现的次数:return:定义一个空数组,存放逻辑处理后的数据list〔〕for循环字符串的数据foriinstrtest:使用count函数,统计出所有字符串出现的次数countstrtest。count(i,0,len(strtest))判断字符串出现的次数与设置的counts的次数相同,则将数据存放在list数组中ifcountnum:list。append(i)返回第n次出现的字符串returnlist〔counts1〕print(test(gbgkkdehh,1,2))结果:d8。判断字符串awelcometomyworld是否包含单词bworld,包含返回True,不包含返回False。deftest():messagewelcometomyworldworldworldifworldinmessage:returnTruereturnFalseprint(test())结果:True9。从0开始计数,输出指定字符串Ahello在字符串Bhihowareyouhelloworld,helloyoyo!中第一次出现的位置,如果B中不包含A,则输出1。deftest():messagehihowareyouhelloworld,helloyoyo!worldhelloreturnmessage。find(world)print(test())结果:1510。从0开始计数,输出指定字符串Ahello在字符串Bhihowareyouhelloworld,helloyoyo!中最后出现的位置,如果B中不包含A,则输出1。deftest(string,str):定义lastposition初始值为1lastposition1whileTrue:positionstring。find(str,lastposition1)ifposition1:returnlastpositionlastpositionpositionprint(test(hihowareyouhelloworld,helloyoyo!,hello))结果:2811。给定一个数a,判断一个数字是否为奇数或偶数。whileTrue:try:判断输入是否为整数numint(input(输入一个整数:))不是纯数字需要重新输入exceptValueError:print(输入的不是整数!)continueifnum20:print(偶数)else:print(奇数)break结果:输入一个整数:100偶数12。输入一个姓名,判断是否姓王。deftest():userinputinput(请输入您的姓名:)ifuserinput〔0〕王:return用户姓王return用户不姓王print(test())结果:请输入您的姓名:王总用户姓王13。如何判断一个字符串是不是纯数字组成?deftest(num):try:returnfloat(num)exceptValueError:return请输入数字print(test(133w3))14。将字符串aThisisstringexample。。。。wow!全部转成大写,字符串bWelcomeToMyWorld全部转成小写。aThisisstringexample。wow!bWelcomeToMyWorldprint(a。upper())print(b。lower())15。将字符串awelcometomyworld首尾空格去掉Python提供了strip()方法,可以去除首尾空格,rstrip()去掉尾部空格,lstrip()去掉首部空格,replace(,)去掉全部空格。awelcometomyworldprint(a。strip())还可以通过递归的方式实现:deftrim(s):flag0ifs〔:1〕:ss〔1:〕flag1ifs〔1:〕:ss〔:1〕flag1ifflag1:returntrim(s)else:returnsprint(trim(Helloworld!))通过while循环实现:deftrim(s):while(True):flag0ifs〔:1〕:ss〔1:〕flag1ifs〔1:〕:ss〔:1〕flag1ifflag0:breakreturnsprint(trim(Helloworld!))16。将字符串sajldjlajfdljfddd,去重并从小到大排序输出adfjl。deftest():sajldjlajfdljfddd定义一个数组存放数据strlist〔〕for循环s字符串中的数据,然后将数据加入数组中foriins:判断如果数组中已经存在这个字符串,则将字符串移除,加入新的字符串ifiinstrlist:strlist。remove(i)strlist。append(i)使用sorted方法,对字母进行排序asorted(strlist)sorted方法返回的是一个列表,这边将列表数据转换成字符串return。join(a)print(test())结果:adfjl17。打印出如下图案(菱形): deftest():n8foriinrange(int(n2),int(n2)1):print(abs(i),abs(nabs(i)2))print(test())结果:18给一个不多于5位的正整数(如a12346),求它是几位数和逆序打印出各位数字。classTest:计算数字的位数deftestnum(self,num):try:定义一个length的变量,来计算数字的长度length0whilenum!0:判断当num不为0的时候,则每次都除以10取整length1numint(num)10iflength5:return请输入正确的数字returnlengthexceptValueError:return请输入正确的数字逆序打印出个位数deftestsorted(self,num):ifself。testnum(num)!请输入正确的数字:逆序打印出数字sortednumnum〔::1〕返回逆序的个位数returnsortednum〔1〕print(Test()。testsorted(12346))结果:119。如果一个3位数等于其各位数字的立方和,则称这个数为水仙花数。例如:153135333,因此153就是一个水仙花数。那么如何求1000以内的水仙花数(3位数)。deftest():fornuminrange(100,1000):inum100jnum1010knum10ifi3j3k3num:print(str(num)是水仙花数)test()20。求123。。。100相加的和。i1forjinrange(101):ijiprint(i)结果:505121。计算12345。。。100的值。deftest(sumto):定义一个初始值sumall0循环想要计算的数据foriinrange(1,sumto1):sumalli(1)(1i)returnsumallifnamemain:resulttest(sumto100)print(result)5022。现有计算公式13233343。。。。。。。n3,如何实现:当输入n5时,输出225(对应的公式:1323334353225)。deftest(n):sum0foriinrange(1,n1):sumi10ireturnsumprint(test(5))结果:22523。已知a的值为hello,b的值为world,如何交换a和b的值,得到a的值为world,b的值为hello?ahellobworldcaabbcprint(a,b)24。如何判断一个数组是对称数组?例如〔1,2,0,2,1〕,〔1,2,3,3,2,1〕,这样的数组都是对称数组。用Python判断,是对称数组打印True,不是打印False。deftest():x〔1,a,0,2,0,a,1〕通过下标的形式,将字符串逆序进行比对ifxx〔::1〕:returnTruereturnFalseprint(test())结果:True25。如果有一个列表a〔1,3,5,7,11〕,那么如何让它反转成〔11,7,5,3,1〕,并且取到奇数位值的数字〔1,5,11〕?deftest():a〔1,3,5,7,11〕逆序打印数组中的数据print(a〔::1〕)定义一个计数的变量count0foriina:判断每循环列表中的一个数据,则计数器中会1count1如果计数器为奇数,则打印出来ifcount2!0:print(i)test()结果:〔11,7,5,3,1〕151126。对列表a〔1,6,8,11,9,1,8,6,8,7,8〕中的数字从小到大排序。a〔1,6,8,11,9,1,8,6,8,7,8〕print(sorted(a))结果:〔1,1,6,6,7,8,8,8,8,9,11〕27。找出列表L1〔1,2,3,11,2,5,3,2,5,33,88〕中最大值和最小值。L1〔1,2,3,11,2,5,3,2,5,33,88〕print(max(L1))print(min(L1))结果:881上面是通过Python自带的函数实现,如下,可以自己写一个计算程序:classTest(object):definit(self):测试的列表数据self。L1〔1,2,3,11,2,5,3,2,5,33,88〕从列表中取第一个值,对于数据大小比对self。numself。L1〔0〕deftestsmallnum(self,count)::paramcount:count为1,则表示计算最大值,为2时,表示最小值:return:for循环查询列表中的数据foriinself。L1:ifcount1:循环判断当数组中的数据比初始值小,则将初始值替换ifiself。num:self。numielifcount2:ifiself。num:self。numielifcount!1orcount!2:return请输入正确的数据returnself。numprint(Test()。testsmallnum(1))print(Test()。testsmallnum(2))结果:88128。找出列表a〔hello,world,yoyo,congratulations〕中单词最长的一个。deftest():a〔hello,world,yoyo,congratulations〕统计数组中第一个值的长度lengthlen(a〔0〕)foriina:循环数组中的数据,当数组中的数据比初始值length中的值长,则替换掉length的默认值iflen(i)length:lengthireturnlengthprint(test())结果:congratulations29。取出列表L1〔1,2,3,11,2,5,3,2,5,33,88〕中最大的三个值。deftest():L1〔1,2,3,11,2,5,3,2,5,33,88〕returnsorted(L1)〔:3〕print(test())结果:〔1,2,2〕30。把列表a〔1,6,2,5,9,4。20,3〕中的数字绝对值从小到大排序。deftest():a〔1,6,2,5,9,4,20,3〕定义一个数组,存放处理后的绝对值数据lists〔〕foriina:使用abs()方法处理绝对值lists。append(abs(i))returnlistsprint(test())结果:〔1,6,2,5,9,4,20,3〕更多python知识分。欢迎留言讨论。