test =["hello","world","yoyo"]# 定义一个空字符串
j =''# 通过 for 循环打印出列表中的数据for i in test:
j = j +"_"+ i
# 因为通过上面的字符串拼接,得到的数据是“_hello_world_yoyo”,前面会多一个下划线_,所以把这个下划线去掉print(j.lstrip("_"))
③ 把字符串 s 中的每个空格替换成”%20”,输入:s = “We are happy.”,输出:“We%20are%20happy.”。
使用 replace 函数,替换字符换即可:
s ='We are happy.'print(s.replace(' ','%20'))
结果:
We%20are%20happy.
④ Python 如何打印 99 乘法表?
for 循环打印:
for i inrange(1,10):for j inrange(1, i+1):print('{}x{}={}\t'.format(j, i, i*j), end='')print()
while 循环实现:
i =1while i <=9:
j =1while j <= i:print("%d*%d=%-2d"%(i,j,i*j),end =' ')# %d: 整数的占位符,'-2'代表靠左对齐,两个占位符
j +=1print()
i +=1
⑤ 从下标 0 开始索引,找出单词 “welcome” 在字符串“Hello, welcome to my world.” 中出现的位置,找不到返回 -1。
deftest():
message ='Hello, welcome to my world.'
world ='welcome'if world in message:return message.find(world)else:return-1print(test())
结果:
7
⑥ 统计字符串“Hello, welcome to my world.” 中字母 w 出现的次数。
deftest():
message ='Hello, welcome to my world.'# 计数
num =0# for 循环 messagefor i in message:# 判断如果 ‘w’ 字符串在 message 中,则 num +1if'w'in i:
num +=1return num
print(test())# 结果2
⑦ 输入一个字符串 str,输出第 m 个只出现过 n 次的字符,如在字符串 gbgkkdehh 中,找出第 2 个只出现 1 次的字符,输出结果:d
⑧ 判断字符串 a = “welcome to my world” 是否包含单词 b = “world”,包含返回 True,不包含返回 False。
deftest():
message ='welcome to my world'
world ='world'if world in message:returnTruereturnFalseprint(test())
结果:
True
⑨ 从 0 开始计数,输出指定字符串 A = “hello” 在字符串 B = “hi how are you hello world, hello yoyo!”中第一次出现的位置,如果 B 中不包含 A,则输出 -1。
deftest():
message ='hi how are you hello world, hello yoyo!'
world ='hello'return message.find(world)print(test())
结果:
15
⑩ 从 0 开始计数,输出指定字符串 A = “hello”在字符串 B = “hi how are you hello world, hello yoyo!”中最后出现的位置,如果 B 中不包含 A,则输出 -1。
deftest(string,str):# 定义 last_position 初始值为 -1
last_position =-1whileTrue:
position = string.find(str, last_position+1)if position ==-1:return last_position
last_position = position
print(test('hi how are you hello world, hello yoyo!','hello'))
结果:
28
⑪ 给定一个数 a,判断一个数字是否为奇数或偶数。
whileTrue:try:# 判断输入是否为整数
num =int(input('输入一个整数:'))# 不是纯数字需要重新输入except ValueError:print("输入的不是整数!")continueif num %2==0:print('偶数')else:print('奇数')break
结果:
输入一个整数:100
偶数
deftrim(s):
flag =0if s[:1]==' ':
s = s[1:]
flag =1if s[-1:]==' ':
s = s[:-1]
flag =1if flag==1:return trim(s)else:return s
print(trim(' Hello world! '))
通过 while 循环实现:
deftrim(s):while(True):
flag =0if s[:1]==' ':
s = s[1:]
flag =1if s[-1:]==' ':
s = s[:-1]
flag =1if flag==0:breakreturn s
print(trim(' Hello world! '))
⑯ 将字符串 s = “ajldjlajfdljfddd”,去重并从小到大排序输出”adfjl”。
deftest():
s ='ajldjlajfdljfddd'# 定义一个数组存放数据
str_list =[]# for循环s字符串中的数据,然后将数据加入数组中for i in s:# 判断如果数组中已经存在这个字符串,则将字符串移除,加入新的字符串if i in str_list:
str_list.remove(i)
str_list.append(i)# 使用 sorted 方法,对字母进行排序
a =sorted(str_list)# sorted方法返回的是一个列表,这边将列表数据转换成字符串return"".join(a)print(test())
结果:
adfjl
⑰ 打印出如下图案(菱形):
deftest():
n =8for i inrange(-int(n/2),int(n/2)+1):print(" "*abs(i),"*"*abs(n-abs(i)*2))print(test())
结果:
********************************
classTest(object):def__init__(self):# 测试的列表数据
self.L1 =[1,2,3,11,2,5,3,2,5,33,88]# 从列表中取第一个值,对于数据大小比对
self.num = self.L1[0]deftest_small_num(self, count):"""
:param count: count为 1,则表示计算最大值,为 2 时,表示最小值
:return:
"""# for 循环查询列表中的数据for i in self.L1:if count ==1:# 循环判断当数组中的数据比初始值小,则将初始值替换if i > self.num:
self.num = i
elif count ==2:if i < self.num:
self.num = i
elif count !=1or count !=2:return"请输入正确的数据"return self.num
print(Test().test_small_num(1))print(Test().test_small_num(2))
结果:
881
㉘ 找出列表 a = [“hello”, “world”, “yoyo”, “congratulations”] 中单词最长的一个。
deftest():
a =["hello","world","yoyo","congratulations"]# 统计数组中第一个值的长度
length =len(a[0])for i in a:# 循环数组中的数据,当数组中的数据比初始值length中的值长,则替换掉length的默认值iflen(i)> length:
length = i
return length
print(test())
结果:
congratulations
deftest():
a =[1,2,3,11,2,5,88,3,2,5,33]# 找到数组中最大的数字
b =max(a)
count =0# 定义一个计数器,每次循环一个数字的时候,则计数器+1,用于记录数字的下标for i in a:
count +=1# 判断当循环到最大的数字时,则退出if i == b:breakreturn count -1print(test())
结果:
6