-
根据完整的路径从路径中分离文件路径、文件名及扩展名
代码呈现:import os def separate_path(full_path): file_path, filename = os.path.split(full_path) basename, extension = os.path.splitext(filename) return file_path, basename, extension full_path = "C:/Users/86189/PycharmProjects/作业集合/文件路径的分离.py" print(separate_path(full_path))
结果呈现:
-
根据标点符号对字符串进行分行;
代码呈现:import re def split_by_punctuation(a): sentences = re.split(r'[。!?]', a) return [sentence for sentence in sentences if sentence] a = "第一句话。第二句话!第三句话?" print("\n".join(split_by_punctuation(a)))
结果呈现:
-
去掉字符串数组中每个字符串的空格;
代码呈现:def remove_spaces(strings): return [s.replace(" ", "") for s in strings] strings = ["hello python", " do you love python ? "] print(remove_spaces(strings))
结果呈现:
-
两个学员输入各自最喜欢的游戏名称,判断是否一致,如果相等,则输出你们俩喜欢相同的游戏;如果不相同,则输 出你们俩喜欢不相同的游戏。
-
上题中两位同学输入 lol和 LOL代表同一游戏,怎么办?
代码呈现(与4):def check_favorite_game(game_1, game_2): if game_1.lower() == game_2.lower(): return "游戏爱好相同!" else: return "游戏爱好不同!" game_1 = input("第一个同学喜欢的游戏: ") game_2 = input("第二个同学喜欢的游戏: ") print(check_favorite_game(game_1, game_2))
结果呈现(与4):
-
让用户输入一个日期格式如“2008/08/08”,将输入的日期格式转换为“2008年-8月-8日”。
代码呈现:def date(date_str): year, month, day = date_str.split('/') return "{}年-{}月-{}日".format(year, int(month), int(day)) date_str = input('请输入XXXX/XX/XX的日期格式:') print(date(date_str))
结果呈现:
-
接收用户输入的字符串,将其中的字符进行排序(升序),并以逆序的顺序输出,“cabed”→"abcde"→“edcba”
代码呈现:def string(s): return "".join(sorted(s)[::-1]) s = input('字母输入(如:cbaed:)') print(string(s))
结果呈现:
-
接收用户输入的一句英文,将其中的单词以反序输 出,“hello c java python”→“python java c hello”。
代码呈现:def reverse_words(sentence): words = sentence.split() return " ".join(words[::-1]) sentence = input() print(reverse_words(sentence))
结果呈现:
-
从请求地址中提取出用户名和域名 http://www.163.com?userName=admin&pwd=123456
【待补充···】 -
有个字符串数组,存储了10个书名,书名有长有短,现 在将他们统一处理,若书名长度大于10,则截取长度8的 子串并且最后添加“...”,加一个竖线后输出作者的名字。
代码呈现:def format_book_titles(book_titles_with_authors): formatted_books = [] for title, author in book_titles_with_authors: if len(title) > 10: title = title[:8] + "..." formatted_book = f"{title} | {author}" formatted_books.append(formatted_book) return formatted_books books_with_authors = [ ("追风筝的人", "卡勒德·胡赛尼"), ("解忧杂货店", "东野圭吾"), ("百年孤独", "加西亚·马尔克斯"), ("活着", "余华"), ("三体:死神永生", "刘慈欣"), ("围城", "钱钟书"), ("挪威的森林", "村上春树"), ("红楼梦", "曹雪芹"), ("简爱", "夏洛蒂·勃朗特 "), ("其实这是一本书名很长的书", "佚名") ] formatted_books = format_book_titles(books_with_authors) for book in formatted_books: print(book)
结果呈现:
-
让用户输入一句话,找出所有"呵"的位置。
代码呈现:def find_positions(sentence, target="呵"): return [pos for pos, char in enumerate(sentence) if char == target] user_input = input("请输入一句话:") positions = find_positions(user_input, "呵") print("‘呵’的位置:", positions)
结果呈现:
-
让用户输入一句话,判断这句话中有没有邪恶,如果有邪恶就替换成这种形式然后输出,如:“老牛很邪恶”,输出后变 成”老牛很**”;
代码呈现:def censor_evil(sentence, word="邪恶"): if word in sentence: return sentence.replace(word, '*' * len(word)) else: return sentence user_input = input("请输入一句话:") censored_sentence = censor_evil(user_input) print("处理后的句子为:", censored_sentence)
结果呈现:
-
判断一个字符是否是回文字符串 "1234567654321" "上海自来水来自海上"
代码呈现:def is_palindrome(s): s = ''.join(c for c in s if c.isalnum()).lower() return s == s[::-1] print(is_palindrome(input()))
结果呈现:
-
过滤某个文件夹下的所有"xx.py"python文件
代码呈现:import os folder_path = 'C:/Users/86189/PycharmProjects/作业集合' python_files = [f for f in os.listdir(folder_path) if f.endswith(".py")] print('Python文件列表:', python_files)
结果呈现:
15.用户管理系统的,密码加密
代码呈现:
import hashlib
def create_user(username, password):
encoded_password = password.encode('utf-8')
hashed_password = hashlib.md5(encoded_password)
password_md5 = hashed_password.hexdigest()
return {'username': username, 'password': password_md5}
def add_user(users, user):
users.append(user)
def find_user_by_username(users, username):
for user in users:
if user['username'] == username:
return user
return None
def main():
users = []
while True:
print("1. 登录")
print("2. 注册")
print("3. 退出")
choice = input("请选择操作:")
if choice == '1':
username = input("请输入用户名:")
password = input("请输入密码:")
encoded_password = password.encode('utf-8')
hashed_password = hashlib.md5(encoded_password)
password_md5 = hashed_password.hexdigest()
user = find_user_by_username(users, username)
if user and user['password'] == password_md5:
print("登录成功!")
else:
print("用户名或密码错误!")
elif choice == '2':
username = input("请输入用户名:")
password = input("请输入密码:")
user = create_user(username, password)
add_user(users, user)
print("注册成功!")
elif choice == '3':
print("再见!")
break
else:
print("无效的选择,请重新输入。")
if __name__ == "__main__":
main()
结果呈现: