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.

  • 相关阅读:
    三毛
    深复制和浅复制
    并发和并行
    PhotoKit保存图片到相册
    利用代码块
    Maven打包程序
    通过Nginx+Tomcat简单实现发布时不间断服务的提供
    C# java MD5加密方不一致问题
    SpringBoot读取配置值的方式
    Java8之集合排序
  • 原文地址:https://www.cnblogs.com/shenfei2031/p/2133998.html
Copyright © 2011-2022 走看看