undefinedfix
Sign in

How to display and calculate characters with ASCII code like C in Python?

marlon edited in Fri, 25 Nov 2022

In C

a=65;
printf("%c",a);

Character variables are equivalent to integer variables, so

a=a+1

Meaningful, i.e

a="B"

But now I know that variables in Python are automatically matched. If I want to add and subtract a single character (that is, to operate as an integer ASCII code), can I simply implement it?

1 Replies
qwiou
commented on Fri, 25 Nov 2022
# 输入字符
a = input("请输入一个字符: ")
print( a + " 的ASCII 码为", ord(a))
b = ord(a)+1
print( a , "ASCII 码 +1 对应字符为", chr(b))
 
# 输入ASCII码,并将输入的数字转为整型
c = int(input("请输入一个ASCII码: "))

print( c , " 对应的字符为", chr(c))
print( c+1 , " 对应的字符为", chr(c+1))

Running environment python3