python 2 与python 3 区别

print

def print(self, *args, sep=' ', end='
', file=None): # known special case of print
    """
    print(value, ..., sep=' ', end='
', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.
    """
    pass

Python2中print是一个语句,只需要向吧输出的放在print关键字后面就可以输出。

Python3中从上面的代码中可以看出他现在是一个函数,就像其他函数一样,print()需要您将要输出的内容作为参数传给他。

Unicode字符串

Python 2有两种字符串类型:Unicode字符串和非Unicode字符串。Python 3只有一种类型:Unicode字符串(Unicode strings)。

1、Python2里的Unicode字符串在Python3里也是普通的字符串,因为在Python里字符串总是Unicode形式的

所以你就可以用下面的方式命名变量了,但是不要这样使用~~

帅哥 = 'll'
print(帅哥)

2、Unicode原始字符串(raw string),使用这种字符串,Python不会自动转移反斜线“”也会被替换为普通的字符串,因为在Python3里所有的原始字符串也都是Unicode编码的

Python 2有两种字符串类型:Unicode字符串和非Unicode字符串。Python 3只有一种类型:Unicode字符串(Unicode strings)

3、Python 2有两个全局函数可以把对象强制转换成字符串:unicode()把对象转换成Unicode字符串,还有str()把对象转换为非Unicode字符串。Python 3只有一种字符串类型,Unicode字符串,所以str()函数即可完成所有的功能。(unicode()函数在Python 3里不再存在了。)

原文地址:https://www.cnblogs.com/lst1010/p/6587678.html