1. 程式人生 > >python中字元與數字的轉換

python中字元與數字的轉換

  與C等語言不同,在python中,是不區分字元與字串的。像C等語言,字元用單引號封裝,字串用雙引號封裝;而python中,單引號和雙引號都表示字串。雖然如此,Python中依舊提供了字元轉數字的方法。
ord(…)
ord(c) -> integer
Return the integer ordinal of a one-character string.
該函式用於將單字元的字串轉化為對應字元的unicode碼值。
該函式的對應的相反函式為chr和unichr
chr(int) -> str
Return a string of one character whose ASCII code is the integer i.
該函式用於將數字轉化為對應的字串。
unicode
Return the Unicode string of one character whose Unicode code is the integer i.
示例程式碼