zoukankan      html  css  js  c++  java
  • Python之str()与repr()的区别

      Python之str()与repr()的区别

      str()一般是将数值转成字符串,主要面向用户。 
      repr()是将一个对象转成字符串显示,注意只是显示用,有些对象转成字符串没有直接的意思。如list,dict使用str()是无效的,但使用repr可以,这是为了看它们都有哪些值,为了显示之用,主要面向python。

    官方文档:


    The str() function is meant to return representations of values which are fairly
    human-readable, while repr() is meant to generate representations which can be read by
    the interpreter (or will force a SyntaxError if there is not equivalent syntax). For
    objects which don't have a particular representation for human consumption, str() will
    return the same value as repr(). Many values, such as numbers or structures like lists
    and dictionaries, have the same representation using either function. Strings and
    floating point numbers, in particular, have two distinct representations.

    Some examples:

    >>> s = 'Hello, world.'
    
    >>> str(s)
    
    'Hello, world.'
    
    >>> repr(s)
    
    "'Hello, world.'"
    
    >>> str(0.1)
    
    '0.1'
    
    >>> repr(0.1)
    
    '0.10000000000000001'
    
    >>> x = 10 * 3.25
    
    >>> y = 200 * 200
    
    >>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'
    
    >>> print s
    
    The value of x is 32.5, and y is 40000...
    
    >>> # The repr() of a string adds string quotes and backslashes:
    
    ... hello = 'hello, world
    '
    
    >>> hellos = repr(hello)
    
    >>> print hellos
    
    'hello, world
    '
    
    >>> # The argument to repr() may be any Python object:
    
    ... repr((x, y, ('spam', 'eggs')))
    
    "(32.5, 40000, ('spam', 'eggs'))"
    
    >>> # reverse quotes are convenient in interactive sessions:
    
    ... `x, y, ('spam', 'eggs')`
    
    "(32.5, 40000, ('spam', 'eggs'))"
  • 相关阅读:
    【转】Maven多模块项目构建
    【转】vue和springboot项目使用nginx配置,并配置跨域
    关于URL与URI
    C#将16位二进制转换为有符号数
    WPF中 datagird日期列格式
    记录一个困扰了我两个周的Windows网络问题
    基于企业上下级关系的组织机构体系数据模型设计
    WPF简介
    和尚挖井故事给程序员的启示!
    看八个笑话故事 悟八个人生道理
  • 原文地址:https://www.cnblogs.com/python-nameless/p/6126014.html
Copyright © 2011-2022 走看看