python 2.7 命令行打印、print、str()、repr()

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liangxy2014/article/details/79225714

**1.**Python打印值(命令行打印):会保持该值在python代码中的状态,不是用户所希望看到的状态

>>> "hello" 
'hello'  #python打印出的值是给python理解的。python理解为字符串,所以带引号   

**2.**Print打印值:打印出来的值是用户所希望看到的状态

>>> print "hello"
hello

**3.**repr():
创建一个字符串,以合法python表达式的形式来表示值。repr()是一个函数。

>>> repr('hello')
"'hello'"
>>> print repr('hello')
'hello'

**4.**str() :
把值转换为合理形式的字符串,给用户看的。str实际上类似于int,long,是一种类型。

>>> print str("Hello,  world!")
Hello,  world!            
>>> print str(1000L)
1000                         
>>> str("Hello, world!")
'Hello, world!'               # 字符串转换之后仍然是字符串
>>> str(1000L)
'1000'

Python 可以将任意值转为字符串:将它传入repr() 或str() 函数

函数str() 用于将值转化为适于人阅读的形式,
而repr() 转化为供机器(解释器)读取的形式
也就是说 repr() 输出对 Python比较友好,而str()的输出对用户比较友好。

猜你喜欢

转载自blog.csdn.net/liangxy2014/article/details/79225714