The problem as defined in your question can be solved as follows:
parts = name.split()
parts[1]=''.join([chr((ord(c) - 65 + 1) % 26 + 65)
if ord(c) < 91 else
chr((ord(c) - 97 + 1) % 26 + 97)
for c in parts[1]])
' '.join(parts)
Here, I define the last name as the second word of the string, this of course is a strong assumption, but improving on this is not the main problem in the question.
Shifting the characters is done inside a list comprehension, where each character is processed separately, and first converted to its ASCII code using ord
. The ASCII codes of upper case letters are 65-90 (A
-Z
), and the ASCII codes of lowercase letters are 97-122 (a
-z
). Therefore, a condition ord(c) < 91
is used to separate the cases. Then, in each case, the ASCII code is converted to a value in the range 0-25, shifted (in the example, incremented by 1), and modulo operation % 26
is used to convert shifted z
back into a
. The resulting value is then converted back to the proper range for the letter ASCII codes.