Python 字符处理方法:str, repr区别
来源:互联网 发布:印刷厂排版软件 编辑:程序博客网 时间:2024/06/05 21:18
Python 字符处理方法:str, repr区别
1.str
与repr
很多时候用 Python
进行输出,我们会把其他类型的值转化成string
进行输出(私以为是Python
注重和人的交互,而string
是最适合与人类进行交互的数据类型),有str()
和repr()
两种方法对数据类型进行转化,str()
转化后的结果更适合与人进行交互,而repr()
转化后的结果则可以被Python
的解释器阅读,但当要转化的对象没有适合与人交互的类型时,str()
转化的结果和repr()
是一样的:
>>> s='hello world'>>> str(s)'hello world'>>> repr(s)"'hello world'">>>
当交互的对象是人时,'hello world'显而易见就是一个字符串,字符串代表的意思是不言而喻,或者说人更关注' '内的信息,而非' '本身,但是机器则不同,如果直接把hello world传给机器,他很难处理这个数据,但是有了' '后,Python
的解释器就知道这是一个字符串,或者也可以这么说,相较于字符串的具体内容,机器更关心的是'hello world'这个整体,所以为了保存所需要的信息,repr()
会给转化的对象加上" "。
>>> x=10>>> s='the value of x is '+repr(x)>>> s'the value of x is 10'>>> u='the value of x is '+str(x)>>> u'the value of x is 10'>>>
对于这种组合类型的变量,str()
和repr()
的结果是一样的。
>>> h='hello \n'>>> print(str(h))hello >>> print(repr(h))'hello \n'>>>
str()
和repr()
的区别可见一斑。
阅读全文
0 0
- Python 字符处理方法:str, repr区别
- 区别python 的str和repr
- python之repr()和str()的区别
- python中str()和repr()区别
- python之repr()和str()的区别
- python 中 str() 与 repr() 的区别
- python中str()与repr()的区别
- python str和repr 的区别
- python str和repr的区别
- python str和repr的区别
- python str与repr的区别
- python str和repr的区别
- python学习 str与repr区别
- Python str 和 repr 的区别
- Python str() 与 repr() 的区别
- str和repr区别
- python-str()和 repr()
- Python repr str 函数
- LSMW、BDC、CATT 区别
- 备忘2017年布尔教育燕十三php视频教
- leetCode--same-tree
- (3)Java环境变量设置和JDK的安装
- 微信小程序开发系列——2. 开发者工具以及开发框架简介
- Python 字符处理方法:str, repr区别
- juc 1
- C++的运算符重载
- Docker image创建之Hello world
- Stm32_调试出现 Error:Flash Download Failed-"Cortex-M3" 解决方案。
- 虚拟机Ubuntu12.04图形界面进不去解决办法
- CSS设置组件的位置
- 基于大数据合成作战系统的设计理念与应用
- 统计信息