总的来说

  • str():将传入的值转换为适合人阅读的字符串形式
  • repr():将传入的值转换为 Python 解释器可读取的字符串形式

传入整型

# number
resp = str(1)
print(resp, type(resp), len(resp))
resp = str(1.1)
print(resp, type(resp), len(resp)) resp = repr(1)
print(resp, type(resp), len(resp))
resp = repr(1.1)
print(resp, type(resp), len(resp)) # 输出结果
1 <class 'str'> 1
1.1 <class 'str'> 3
1 <class 'str'> 1
1.1 <class 'str'> 3

传入字符串

# string
resp = str("test")
print(resp, type(resp), len(resp)) resp = repr("test")
print(resp, type(resp), len(resp)) # 输出结果
test <class 'str'> 4
'test' <class 'str'> 6

repr() 会在原来的字符串上面加单引号,所以字符串长度会 +2

Python - repr()、str() 的区别的更多相关文章

  1. Python repr, str, eval 使用小记 及 str 和 repr的区别

    >>> s = '1+2'>>> x = eval(s) #把引号剥离一次,就变成了运算1+2>>> x3>>> ss = st ...

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

    Python之str()与repr()的区别 str()一般是将数值转成字符串,主要面向用户.  repr()是将一个对象转成字符串显示,注意只是显示用,有些对象转成字符串没有直接的意思.如list, ...

  3. Python中str()与repr()函数的区别——repr() 的输出追求明确性,除了对象内容,还需要展示出对象的数据类型信息,适合开发和调试阶段使用

    Python中str()与repr()函数的区别 from:https://www.jianshu.com/p/2a41315ca47e 在 Python 中要将某一类型的变量或者常量转换为字符串对象 ...

  4. python 打印 str 字符串的实际内容 repr(str)

    python 打印 str 字符串的实际内容 repr(str) s = 'aa' print(repr(s))

  5. python的str,unicode对象的encode和decode方法, Python中字符编码的总结和对比bytes和str

    python_2.x_unicode_to_str.py a = u"中文字符"; a.encode("GBK"); #打印: '\xd6\xd0\xce\xc ...

  6. python的str,unicode对象的encode和decode方法

    python的str,unicode对象的encode和decode方法 python中的str对象其实就是"8-bit string" ,字节字符串,本质上类似java中的byt ...

  7. python的str,unicode对象的encode和decode方法(转)

    python的str,unicode对象的encode和decode方法(转) python的str,unicode对象的encode和decode方法 python中的str对象其实就是" ...

  8. bytes和str的区别与转换

    bytes和str的区别 1.英文 b'alex'的表现形式与str没什么两样 2.中文 b'\xe4\xb8\xad'这是一个汉字在utf-8的bytes表现形式 3.中文 b'\xce\xd2'这 ...

  9. JSON.parseObject(String str)与JSONObject.parseObject(String str)的区别

    一.首先来说说fastjson fastjson 是一个性能很好的 Java 语言实现的 JSON 解析器和生成器,来自阿里巴巴的工程师开发.其主要特点是: ① 快速:fastjson采用独创的算法, ...

  10. Python repr() 函数

    Python repr() 函数  Python 内置函数 描述 repr() 函数将对象转化为供解释器读取的形式. 语法 以下是 repr() 方法的语法: repr(object) 参数 obje ...

随机推荐

  1. springboot集成swagger的pom依赖

    pom依赖加入以下内容 //版本一致做个属性 <properties> <swagger.version>2.6.1</swagger.version> </ ...

  2. 关于Linux下Texlive无法找到已安装字体的问题与解决

    关于Linux下Texlive无法找到已安装字体的问题与解决 当我在Ubuntu系统下使用Latex时,在编译渲染时报出了Font "xxx" does not contain r ...

  3. 学习vue过程中遇到的问题

    1.vue-quill-editor动态禁用 项目中把vue-quill-editor单独封装成了一个组件,通过props传递readOnly参数来设置是否禁用editor.开发中发现可以实现禁用效果 ...

  4. VScode安装配置

    一.安装VScode 进入VScode官网Visual Studio Code下载 安装 二.设置中文 打开vscode 重启vscode 三.美化 四.安装拓展插件 Auto Close Tag ( ...

  5. Build Web Server with Apache and Passenger

    Follow the instructions at 2.6. Generic installation, upgrade and downgrade method: via tarball of P ...

  6. 一键部署lamp脚本

    #!/bin/bash systemctl stop firewalld systemctl disable firewalld setenforce 0 #-------Apache------ # ...

  7. scrapy爬虫框架使用

    一.scrapy框架 1.什么是scrapy: 爬虫中封装好的一个明星框架.功能:高性能的持久化存储,异步的数据下载,高性能的数据解析,分布式. 2.使用方法: 安装: 下载tiwisted,此处位下 ...

  8. [源码解析] 机器学习参数服务器Paracel (3)------数据处理

    [源码解析] 机器学习参数服务器Paracel (3)------数据处理 目录 [源码解析] 机器学习参数服务器Paracel (3)------数据处理 0x00 摘要 0x01 切分需要 1.1 ...

  9. 绕WAF常见思路整理(一)

    最*被*台的一些事情搞得心态有点崩,很久没写文了 *期想整理一下常见的各种操作中绕过WAF的思路与免杀的思路(这部分之前没整理完以后有机会再说),受限于个人水*因素所以一定是不完全的,而且在WAF日新 ...

  10. SpringBoot返回枚举对象中的指定属性

    枚举 package com.meeno.boot.oa.employee.enums; import com.alibaba.fastjson.annotation.JSONType; import ...