zoukankan      html  css  js  c++  java
  • Python ,数字与字符,数字与字符串

    数字变为字符串 str()
    字符串变为数字 string.atoi(s,[,base]) //base为进制基数
    浮点数转换 string.atof(s)

    需求:
    需要把一个字符(ASCII或Unicode)转换为数字编码

    ,或者反过来转换.
    讨论:
    对于ASCII字符,可以使用内建的ord和chr方法实现需求:
    >>> chr(97)
    'a'
    >>> ord('a')
    97
    对于Unicode字符,需要使用ord和repr,获得unicode字符的方法,使用unichr:
    >>> print ord(u'\u2020')
    8224
    >>> print repr(unichr(8224))
    u'\u2020'
    相关说明:
    下面是python中对这几个方法的简单说明:
    ord(...)
        ord(c) -> integer
        Return the integer ordinal of a one-character string.
    chr(...)
        chr(i) -> character
        Return a string of one character with ordinal i; 0 <= i < 256.
    repr(...)
        repr(object) -> string
        Return the canonical string representation of the object.
        For most object types, eval(repr(object)) == object.
    unichr(...)
        unichr(i) -> Unicode character
        Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.

  • 相关阅读:
    【loj6179】Pyh的求和
    【bzoj4036】按位或
    【CF472G】Design Tutorial: Increase the Constraints
    【bzoj4811】由乃的OJ
    双马尾机器人(???)
    【codechef】Children Trips
    【bzoj3796】Mushroom追妹纸
    【bzoj4571】美味
    前夕
    【bzoj3589】动态树
  • 原文地址:https://www.cnblogs.com/shenfei2031/p/2133998.html
Copyright © 2011-2022 走看看