完成作业321
This commit is contained in:
20
题目1.py
Normal file
20
题目1.py
Normal file
@@ -0,0 +1,20 @@
|
||||
word = "Hello"
|
||||
print("1. 使用 ord() 函数:")
|
||||
for char in word:
|
||||
ascii_val = ord(char)
|
||||
print(f" '{char}' -> {ascii_val}")
|
||||
|
||||
print("\n2. 使用 chr() 函数验证:")
|
||||
|
||||
ascii_value = 65
|
||||
char_from_ascii = chr(ascii_value)
|
||||
print(f" ASCII码 {ascii_value} -> 字符 '{char_from_ascii}'")
|
||||
|
||||
print("\n3. 完整转换验证:")
|
||||
print(" 字符串 'Hello' 的ASCII码列表:")
|
||||
ascii_list = [ord(c) for c in word]
|
||||
print(f" {ascii_list}")
|
||||
|
||||
print(" 从ASCII码列表恢复字符串:")
|
||||
recovered_word = ''.join(chr(code) for code in ascii_list)
|
||||
print(f" {ascii_list} -> '{recovered_word}'")
|
||||
Reference in New Issue
Block a user