简体   繁体   English

如何将字符串的最后一个字符替换为字母表的第一个字母?

[英]How can I replace the last character of a string for the first letter of the alphabet?

I am currently working on a code that moves every letter from a word, one letter up, for example: Abc becomes Bcd, (The first letter is always capital), but whenever I type z in a word then it becomes { or [ (if it is a capital letter).我目前正在编写一个代码,将单词中的每个字母,一个字母向上移动,例如:Abc 变为 Bcd,(第一个字母始终为大写),但是每当我在单词中键入 z 时,它就会变为 { 或 [ (如果是大写字母)。 For example, if I type zoo, the output is "[pp" I want z to become a.例如,如果我输入 zoo,输出是“[pp”我希望 z 成为 a。 So far I have tried this:到目前为止,我已经尝试过这个:

 word = input("Insert Word:")
 word = word.capitalize().replace(" ", "")

for shift in word:
  incrypt = chr(ord(shift)+1)
  print (incrypt, end='')

I have also tried using if statements like this:我也试过使用这样的 if 语句:

word = input("Insert Word:")
word = word.capitalize().replace(" ", "")
    for shift in word:
  incrypt = chr(ord(shift)+1)
  Last_char= incrypt[-1]
  if Last_char > chr(123):
    Last_char = [chr(97)]
    print (incrypt, end='')
  elif Last_char == chr(91):
    Last_char = [chr(65)]
    print (incrypt, end='')

But it didn't work.但它没有用。

Here is a solution using join:这是使用 join 的解决方案:

word = word.lower()

"".join(chr(ord('a') + (ord(w)-ord('a')+1)%26) for w in word).capitalize()

I would do this :我会这样做:

word = input("Insert Word:")
word = word.capitalize().replace(" ", "")

A,Z = ord('A'), ord('Z')
a,z = ord('a'), ord('z')

step = 1

for shift in word:
    new_char = ord(shift) + step
    
    incrypt = None
    if 'a' <= shift <= 'z':
        incrypt = chr(new_char) if new_char <= z else chr(a + (new_char - z - 1)%26 )
    elif 'A' <= shift <= 'Z':
        incrypt = chr(new_char) if new_char <= Z else chr(A + (new_char - Z - 1)%26 )
    else : 
        incrypt = shift
    print (incrypt, end='')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据第一个和最后一个字符替换字符串 - How to replace a string based on first and last character 我如何在python中编写程序以将字符串中的字符替换为其他字符,而不考虑大小写 - How can I write a program in python to replace character in string with other character irrespective of case-letter 如果字符是 X,则替换字符串中的第一个和最后一个字符 - Replace first and last character in string if the character is X 如何找到字符串中字母(来自字母表)第一次出现的位置? - How do I find the position of the first occurrence of a letter(from the alphabet) in a string? 是否存在检查字符串中的字符是否为字母中的字母的函数? (迅速) - Is there a function that checks if a character in a string is a letter in the alphabet? (Swift) 如何替换字符串中第一个字母的所有实例,而不替换第一个字母本身 - How do I replace all instances of the first letter in a string, but not the first letter itself 如何删除字符串的第一个和最后一个字母? - How to remove the first and last letter of a string? 如何将字符串的第一个和最后一个字母大写? - How to capitalize the first and last letter of a string? 除了第一个和最后一个字母,如何替换字符串中每个字母的出现? - How do you replace every occurrence of a letter in a string apart from the first and the last ones? 如何用字符串中的数字替换字符? - How can I replace a character with a number in a string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM