s=input() new_s = s[-1]+s[:-1] new_s = ' '.join([chr(ord(c)+1) if c.isalpha()else c for c in new_s]) print(new_s)为什么输入abc,输出结果是dbc
时间: 2024-05-20 20:10:33 浏览: 8
这段代码将输入的字符串s的最后一个字符取出来,然后对其进行了一些处理,最后输出处理后的结果。
具体来说,对于最后一个字符,如果它是字母,则将其ASCII码值加1,否则保持不变。然后将处理后的字符与原来的字符串s除了最后一个字符之外的部分拼接起来,得到新的字符串new_s。
针对输入"abc",代码首先取出最后一个字符"c",将其ASCII码值加1得到"d",然后将"d"和原字符串"ab"拼接起来,得到"dbc",这就是为什么输出结果是"dbc"的原因。
相关问题
def fpe(frame_phone_input,root): phone_input = frame_phone_input.get() def fpe_encrypt(plaintext, key, radix=10, rounds=4): # 对明文进行哈希 hash_val = hmac.new(key.encode(), plaintext.encode(), hashlib.sha256).digest() # 将哈希值转换为整数 num_val = int.from_bytes(hash_val, byteorder='big') # 将整数转换为指定进制的字符串 str_val = '' while num_val > 0: num_val, rem = divmod(num_val, radix) str_val = str(rem) + str_val # 对字符串进行 FPE 加密 for i in range(rounds): str_val = ''.join([chr((ord(c) - ord('0') + i) % (radix - 1) + ord('0')) for c in str_val]) # 返回加密后的字符串 return str_val key = 'my-secret-key' encrypted = fpe_encrypt(phone_input, key) phone_label = tk.Label(root, text=("加密后的手机号:", encrypted), font=("微软雅黑 -20")) phone_label.place(x=500, y=560)怎么把加密后的手机号分三行显示
你可以使用字符串的切片功能将加密后的手机号分成三段,然后分别显示在三个 Label 中。下面是一个修改过的代码示例:
```
def fpe(frame_phone_input, root):
phone_input = frame_phone_input.get()
def fpe_encrypt(plaintext, key, radix=10, rounds=4):
# 对明文进行哈希
hash_val = hmac.new(key.encode(), plaintext.encode(), hashlib.sha256).digest()
# 将哈希值转换为整数
num_val = int.from_bytes(hash_val, byteorder='big')
# 将整数转换为指定进制的字符串
str_val = ''
while num_val > 0:
num_val, rem = divmod(num_val, radix)
str_val = str(rem) + str_val
# 对字符串进行 FPE 加密
for i in range(rounds):
str_val = ''.join([chr((ord(c) - ord('0') + i) % (radix - 1) + ord('0')) for c in str_val])
# 返回加密后的字符串
return str_val
key = 'my-secret-key'
encrypted = fpe_encrypt(phone_input, key)
# 将加密后的手机号分成三段
chunk_size = len(encrypted) // 3
chunk1 = encrypted[:chunk_size]
chunk2 = encrypted[chunk_size:2*chunk_size]
chunk3 = encrypted[2*chunk_size:]
# 在三个 Label 中分别显示加密后的手机号的三段
phone_label1 = tk.Label(root, text=("加密后的手机号:", chunk1), font=("微软雅黑 -20"))
phone_label1.place(x=500, y=560)
phone_label2 = tk.Label(root, text=chunk2, font=("微软雅黑 -20"))
phone_label2.place(x=500, y=590)
phone_label3 = tk.Label(root, text=chunk3, font=("微软雅黑 -20"))
phone_label3.place(x=500, y=620)
```
这样,加密后的手机号就会分成三行显示。
1. 输入一个字符串,将字符串中所有的小写字母变成对应的大写字母输出(用upper方法和自己写算法两种方式实现)。例如,输入“Abc123def”,输出“ABC123DEF”。 2. 写程序实现字符串函数find的功能(获取字符串1中字符串2第一次出现的位置) 3. 获取两个字符串中公共的字符。例如字符串1为“abc13”,字符串2为“mna3”,输出的公共字符为“a3”。 4. 假设有一个字符串“abcmmnbcdacbhk”,请去除这个字符串中的重复字符,且把剩余的字符按照字母升序排序输出。从以上实验中能学到什么
1. 用upper方法实现:
```
s = input("请输入一个字符串:")
s = s.upper()
print(s)
```
用自己写的算法实现:
```
s = input("请输入一个字符串:")
new_s = ''
for c in s:
if 'a' <= c <= 'z':
new_s += chr(ord(c) - 32)
else:
new_s += c
print(new_s)
```
2. 实现字符串函数find的功能:
```
def find(str1, str2):
for i in range(len(str1) - len(str2) + 1):
if str1[i:i+len(str2)] == str2:
return i
return -1
s1 = input("请输入字符串1:")
s2 = input("请输入字符串2:")
pos = find(s1, s2)
if pos == -1:
print("字符串1中不包含字符串2")
else:
print("字符串2在字符串1中第一次出现的位置是:", pos)
```
3. 获取两个字符串中公共的字符:
```
s1 = input("请输入字符串1:")
s2 = input("请输入字符串2:")
common = ''
for c in s1:
if c in s2 and c not in common:
common += c
print("两个字符串中公共的字符是:", common)
```
4. 去除字符串中的重复字符并按字母升序排序:
```
s = input("请输入一个字符串:")
s = ''.join(sorted(set(s)))
print(s)
```
以上实验中,我们学会了字符串的一些基本操作,如大小写转换、子串查找、公共字符查找、去重和排序等。同时,也学会了如何使用循环和判断等语句处理字符串中的每个字符,这对于程序设计和开发非常重要。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)