赞
踩
str = "this is a test of Python"
print(str.count("s"))
print(str.find("test"))
print(str[10:14])
# print(str[::-1])
order = []
for i in str:
order.append(i)
order.reverse() #将列表反转
print(''.join(order)) #将list转换成字符串
print(str.replace('test','exam'))
a = "aAsmr3idd4bgs7Dlsf9eAF"
print(a.swapcase())
import re
print(''.join(re.findall("\d+",a)))
a = a.lower()
n = {}
for x in a:
if x.isdigit():
continue
if x in n:
n[x] = n[x] + 1
else:
n[x] = 1
print(n)
import collections a = "aAsmr3idd4bgs7Dlsf9eAF" # 统计字符出现的次数 result_dict = collections.Counter(a) # 转为列表选出最大的值 result_list = list(result_dict.values()) max_value = max(result_list) # 定义一个最大值的列表(可能会出现多个字母都相同频率) max_list = list() # 遍历排序后的把键和值存到列表里 for k,v in result_dict.items(): if v == max_value: max_list.append(k) # 重新对最大的值排序并打印 max_list = sorted(max_list) for paremeter in max_list: print(paremeter,max_value)
a = "aAsmr3idd4bgs7Dlsf9eAF" #字符串
a = list(set(a.lower())) #先把大小写统一,再去重
flag =0 #循环条件
for s in "boy": #设置遍历查询对象
for ai in a:
if s == ai: #如果匹配到一次,flag加一
flag+=1
if flag == 3: #当三个字母全找到时,flag=3
print(True)
else:
print(False)
import re
str = input("请输入字符串:")
num = re.findall(r"\d+\.?\d*", str)
sum1 = 0
for i in num:
sum1+=float(i)
print(sum1)
str = input("请输入一个字符串:")
n = {}
for x in str:
if x in n:
n[x] = n[x] + 1
else:
n[x] = 1
# print(n)
# print(max(n,key=n.get))
# print(max(str,key=str.count))
print(list(n.keys())[list(n.values()).index(max(n.values()))],max(n.values()),sep=":")
def num():
a= int(input("请输入一个整数:"))
n= input("请输入几个数相加:")
s=0
m=0
if n.isdigit(): #判断输入的n是不是数字
n = int(n)
for i in range(n):
m=m+(10**i)*a #求每个单独的数字是多少
s=s+m #把所得的数字累加到一起
print(s)
else:
print("输入错误")
num()
str1 = input("请输入字符串:") def enc(str1): len1 = len(str1) list1 = list(str1) for i in range(len1): s = ord(list1[i]) if (97 <= s <= 120): s += 3 print(chr(s), end="") elif (s == 120): print("a", end="") elif (s == 122): print("c", end="") else: print(chr(s), end="") enc(str1)
import random def veri(n): x = 0 while(x<n): s=random.choice(range(1,4)) if(s==1): a=random.choice(range(1,10)) print(a,end='') x += 1 if(s==2): b=random.choice(range(65,90)) print(chr(b),end='') x+=1 if(s==3): c=random.choice(range(97,122)) print(chr(c),end='') x+=1 n=int(input('输入验证码长度(位数):')) veri(n)
&
import random
def makeCode(length = 4):
str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
code = ""
i = 0
while i < length:
code += str[random.randint(0,len(str)-1)]
i+=1
return code
c = makeCode()
print("生成的随机验证码为:",c)
def contrast():
num1 = int(input("请输入第一个整数:"))
num2 = int(input("请输入第二个整数:"))
if num1 > num2:
print("1")
elif num1 < num2:
print("-1")
elif num1 == num2:
print("0")
contrast()
def prime():
num1 = int(input("请输入一个正整数:"))
if num1 < 2:
print("%d不是质数!"%num1)
else:
for i in range(2,num1):
if num1 % i == 0:
print("%d不是质数!"%num1)
break
else:
print("%d是质数!"%num1)
prime()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。