1.根据完整的路径从路径中分离文件路径、文件名及扩展名
file_path = "C:\\Program Files (x86)\\Fastpic\\fastpic_data\\fastpic_db.dat"
print(file_path)
ls = file_path.split("\\")
print(ls)
file = ls[4:]
print(file)
na_ex = ls[len(ls)-1].split(".")
print(na_ex[1:])
C:\Program Files (x86)\Fastpic\fastpic_data\fastpic_db.dat
['C:', 'Program Files (x86)', 'Fastpic', 'fastpic_data', 'fastpic_db.dat']
['fastpic_db.dat']
['dat']
2.根据标点符号对字符串进行分行
z = input("请输入字符串:")
a = z.replace(",", "\n")
print(a)
请输入字符串:你干啥呢,有事就说,别瞎哔哔
你干啥呢
有事就说
别瞎哔哔
3.去掉字符串数组中每个字符串的空格
s = ["p y t h o n", "l o v e", "y o u"]
s2 = []
for i in s:
ls = i.split(" ")
ls2 ="".join(ls)
s2.append(ls2)
print(s2)
['python', 'love', 'you']
4.随意输入你心中想到的一个书名,然后输出它的字符串长度。 (len()属性:可以得字符串的长度)
book = input("请输入你心中想到的书名:")
print(len(book))
请输入你心中想到的书名:活着
2
5.两个学员输入各自最喜欢的游戏名称,判断是否一致,如果相等,则输出你们俩喜欢相同的游戏;如果不相同,则输出你们俩喜欢不相同的游戏。
student1 = input("学员1输入自己喜欢的游戏名称:")
student2 = input("学员2输入自己喜欢的游戏名称:")
if student1 == student2:
print("你们俩喜欢相同的游戏")
else:
print("你们俩喜欢不相同的游戏")
学员1输入自己喜欢的游戏名称:吃鸡
学员2输入自己喜欢的游戏名称:王者
你们俩喜欢不相同的游戏
6.上题中两位同学输入 lol和 LOL代表同一游戏,怎么办?
student1 = input("学员1输入自己喜欢的游戏名称:")
student2 = input("学员2输入自己喜欢的游戏名称:")
m = student2.lower()
if student1 == student2 or student1 == m:
print("你们俩喜欢相同的游戏")
else:
print("你们俩喜欢不相同的游戏")
学员1输入自己喜欢的游戏名称:lol
学员2输入自己喜欢的游戏名称:LOL
你们俩喜欢相同的游戏
7.让用户输入一个日期格式如“2008/08/08”,将 输入的日期格式转换为“2008年-8月-8日”。
data = input("请输入一个日期格式如“2008/08/08”的日期:")
s = data.split("/")
print(s)
print(f"{s[0]}年-{s[1]}月-{s[2]}日")
请输入一个日期格式如“2008/08/08”的日期:2008/08/08
['2008', '08', '08']
2008年-08月-08日
8.接收用户输入的字符串,将其中的字符进行排序(升序),并以逆序的顺序输出,“cabed”→"abcde"→“edcba”。
word = input("请输入一个字符串")
word_list = list(word)
word_list.sort()
print("".join(word_list))
word_list.reverse()
print("".join(word_list))
请输入一个字符串acdeb
abcde
edcba
9.接收用户输入的一句英文,将其中的单词以反序输出,“hello c sharp”→“sharp c hello”。
a = input("请输入字符串:")
b = a.split(" ")
print(b)
c = b[2::-1]
print(" ".join(c))
请输入字符串:hello c sharp
['hello', 'c', 'sharp']
sharp c hello
10.从请求地址中提取出用户名和域名http://www.163.com?userName=admin&pwd=123456
c = " http://www.163.com?userName=admin&pwd=123456"
a = c.split("?")
b = a[1].split("&")
print(f"用户名是:{b[0]},域名是{a[0]}")
用户名是:userName=admin,域名是 http://www.163.com
12.有个字符串数组,存储了10个书名,书名有长有短,现在将他们统一处理,若书名长度大于10,则截取长度8的子串并且最后添加“...”,加一个竖线后输出作者的名字。
st = ["红与黑", "鲁滨逊漂流记", "汤姆叔叔的小屋:卑贱者的生活", "活着", "悲惨世界", "本草纲目", "红楼梦", "安娜·卡列尼娜", "格列佛游记"]
for i in st:
if len(i) > 10:
print(i)
auther = input("请输入作者姓名:")
x = i[:9]
j = x +(f"... |{auther}")
print(j)
汤姆叔叔的小屋:卑贱者的生活
请输入作者姓名:章章
汤姆叔叔的小屋:卑... |章章
13.让用户输入一句话,找出所有"呵"的位置。
ords = input("请用户输入一句话:")
for i in words:
if i == "呵":
print(words.find(i))
请用户输入一句话:能不能不要在这里呵了
8
14.让用户输入一句话,找出所有"呵呵"的位置。
words = input("请用户输入一句话:")
for i in range(0,len(words)):
if words[i] == "呵" and words[i+1] == "呵":
print(f"呵呵的位置在{i} {i+1}")
请用户输入一句话:呵呵,真好笑
呵呵的位置在0 1
15.让用户输入一句话,判断这句话中有没有邪恶,如果有邪恶就替换成这种形式然后输出,如:“老牛很邪恶”,输出后变成”老牛很**”;
words = input("请用户输入一句话:")
print(words.replace("邪恶", "**"))
请用户输入一句话:老牛很邪恶
老牛很**
16.如何判断一个字符串是否为另一个字符串的子串
find()
index()
双层循环完成
??
def judge(x:str, y:str):
# 方法一 find
res = b.find(a)
if res != -1:
print("a是b的子串")
else:
print("a不是b的子串,b也不是a的子串")
def judge2(x:str, y:str):
# 方法二 index
try:
b.index(a)
print("a是b的子串")
except(ValueError):
print("a不是b的子串")
def judge3(x:str, y:str):
# 方法三 in
if x in y:
print("a是b的子串")
else:
print("a不是b的子串")
b = "this is your name"
a = "name"
# judge(a, b)
# judge2(a, b)
judge3(a, b)
a是b的子串
17.如何验证一个字符串中的每一个字符均在另一个字符串
中出现过
def in_str(x:str, y:str):
ls = []
ls2 = []
for i in x:
ls.append(i)
for j in y:
if j in ls:
ls2.append(j)
if len(y) == len(ls2):
print("b中字符都在a中")
else:
print("b中字符不都在a中或者都不在a中")
a = input("请输入字符串a:")
b = input("请输入字符串b:")
in_str(a, b)
请输入字符串a:123456
请输入字符串b:523
b中字符都在a中
18.如何随机生成无数字的全字母的字符串
def al_string(len):
code_list = []
for i in range(10): # 0-9数字
code_list.append(str(i))
for i in range(65, 91): # 对应从“A”到“Z”的ASCII码
code_list.append(chr(i))
for i in range(97, 123): # 对应从“a”到“z”的ASCII码
code_list.append(chr(i))
myslice = random.sample(code_list, len)
print(myslice)
str_list = []
for i in myslice:
if not i.isdigit():
str_list.append(i)
verification_code = ''.join(str_list)
return verification_code
code = al_string(10)
print(code)
['2', 'q', 'S', '3', 'R', 'U', 'W', 'e', 'l', 'v']
qSRUWelv
19.如何随机生成带数字和字母的字符串
def al_string02(len):
code_list = []
for i in range(10): # 0-9数字
code_list.append(str(i))
for i in range(65, 91): # 对应从“A”到“Z”的ASCII码
code_list.append(chr(i))
for i in range(97, 123): # 对应从“a”到“z”的ASCII码
code_list.append(chr(i))
myslice = random.sample(code_list, len)
verification_code = ''.join(myslice)
return verification_code
code = al_string02(10)
print(code)
9SWIrGALn6
20.如何判定一个字符串中既有数字又有字母
def jud(x:str):
al = []
num = []
for i in x:
if i.isalpha():
al.append(i)
elif i.isdigit():
num.append(i)
if len(al) > 0 and len(num) > 0:
print("这串字符串既有数字还有字母")
else:
print("这串字符串不符合条件")
a = input("请输入一串字符串:")
jud(a)
请输入一串字符串:zddh34gb5gg
这串字符串既有数字还有字母
21.字符串内的字符排序(只按字母序不论大小写)
def str_sort(x:str):
a = x.lower()
c = list(a)
c.sort()
print("".join(c))
z = input("请输入一串字符串:")
str_sort(z)
请输入一串字符串:ADcBe
abcde
22.判断一个字符是否是回文字符串(面试题)
"1234567654321"
"上海自来水来自海上"
def huiwen(x:str):
for i in range(len(x)):
if x[i] != x[len(x)-i-1]:
return print(f"{x}不是回字符串")
return print(f"{x}是回字符串")
x = input("请输入一串字符串:")
huiwen(x)
请输入一串字符串:上海自来水来自海上
上海自来水来自海上是回字符串
作业(选做题):
某个人进入如下一个棋盘中,要求从左上角开始走,
最后从右下角出来(要求只能前进,不能后退),
问题:共有多少种走法?
0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0
1 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0