字符和字符值之間的轉換

  1. 獲得單個字符的字符值
    >>> print ord('a')
    97

  2. 將ASCII碼轉換爲單個字符
    >>> print chr(97)
    'a'

  3. 將Unicode字符串轉換爲Unicode碼
    >>> print ord(u'\u2020')
    8224

  4. 將Unicode碼值轉換爲長度唯一的Unicode字符串
    >>> print repr(unichr(8224))
    u'\u2020'

  5. 將字符串轉換爲一個包含各個字符值得列表
    >>> print map(ord, 'ciao')
    [99, 105, 97, 111]

  6. 將包含字符值的列表創建一個字符串
    >>> print ''.join(map(chr, range(97, 100)))
    abc
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章