python中的print

来源:互联网 发布:怎样淘宝上降价买东西 编辑:程序博客网 时间:2024/05/18 02:58

python3 中去除了print语句,加入print()函数实现相同的功能
print() 会在输出窗口中显示一些文本。

>>> print "hello,world!"SyntaxError: Missing parentheses in call to 'print'>>> print("hello,world!")hello,world!>>> print("你好,中国,早安!")你好,中国,早安!

下面我们来介绍print中的内置方法sep和end

>>> help(print)Help on built-in function print in module builtins:print(...)    print(value, ..., sep=' ', end='\n', 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.

sep—在字符串之间插入值,默认是一个空格,如:

# print中,每个字符串是用“,”逗号隔开的,默认是一个空格>>> print('我是第一个字符串', '我是第二个字符串')我是第一个字符串 我是第二个字符串# 如果我想让两个字符串中没有空格,就可以使用sep了>>> print('我是第一个字符串', '我是第二个字符串', sep='')我是第一个字符串我是第二个字符串#当然,也可以使用其他的文字来隔开字符串。>>> print('我是第一个字符串', '我是第二个字符串', sep='---我的存在,就是为了隔开你们---')我是第一个字符串---我的存在,就是为了隔开你们---我是第二个字符串# 使用逗号来隔开字符串,为了效果明显,估计多加了很多逗号~>>> print('I', 'love', 'Python', sep=',,,,,')I,,,,,love,,,,,Python# \n是换行符>>> print('我是第一个字符串', '我是第二个字符串', sep='\n')我是第一个字符串我是第二个字符串

end—在字符串结尾追加一个值,默认是换行,如:

#输入print点击回车,就直接输出了,如果使用分号,就可以在一行写上两个指令在输出>>> print('我在第一行!');print('我在第二行!')我在第一行!我在第二行!# 如果不加end的话,最后一个值默认是\n,但是如果加了end=‘’,就把\n给去掉了,变成一个空的字符串,所以两个print就可以在一行显示了~>>> print('我在第一行!', end='');print('我在第二行!')我在第一行!我在第二行!>>> print('我在第一行!', end='---就是不让换行---');print('我在第二行!')我在第一行!---就是不让换行---我在第二行!

如果我需要在一个字符串中嵌入一个双引号,可以这样做:

# 我们在"双引号"中,加上"双引号"的字符串,Python会以为这段话已经结束了,以为下个"双引号"开始输入下一段话,所以'它'会很善解人意的提示你“语法错误”>>> print("Python我非常喜欢"",你们喜欢吗?")SyntaxError: invalid syntax# 我们可以使用反斜杠来把"双引号"给注释掉>>> print("Python我非常喜欢\"它\",你们喜欢吗?")Python我非常喜欢"它",你们喜欢吗?# 我们可以在'单引号'中输入字符串,然后字符串中需要加"双引号"的位置使用双引号>>> print('Python我非常喜欢"它",你们喜欢吗?')Python我非常喜欢"它",你们喜欢吗?# 当然,我们也可以在双引号中,输入单引号>>> print("What's your name? \nMy name's Python")What's your name? My name's Python

输入 >>>’Python’与输入 >>>print(‘Python’) 有何不同?

# 输出结果是有引号的>>> 'Python''Python'# 输出结果没有引号>>> print('Python')Python

在python中的计算,甚至可以不用print()函数,可以直接输入数字计算

# 相加>>> 5 + 813# 相减,并且输出负数>>> 5 - 8-3# 相减, 输出是正数>>> 9 - 54# 相乘>>> 2 * 36# 相除,输出是个浮点型的小数>>> 9 / 33.0# 如果想要输出整数,需要用两个除号“//”>>> 9 // 33

还可以用来字符串的相加

>>> # 将字符串相加>>> 'I' + 'love' + 'Python''IlovePython'>>> # 字符串相加后太丑了,我们在单词后面加上空格>>> 'I' + ' ' + 'love' + ' ' + 'Python' + '!''I love Python!'>>> # 中文与英文相加>>> "我" + "爱" + "Python"'我爱Python'

Python不仅就字符串拼接、相加这么简单,还可以使用字符串相乘:

>>> 'I love Python! ' * 3'I love Python! I love Python! I love Python! '

但是其他的可能会报错,比如字符串与数字相加,字符串与字符串相乘,字符串相减,字符串相除。如下

#字符串与数字相加>>> 'I love Python' + 5Traceback (most recent call last):  File "<pyshell#7>", line 1, in <module>    'I love Python' + 5TypeError: Can't convert 'int' object to str implicitly#字符串与字符串相乘>>> 'I love Python!' * 'love'Traceback (most recent call last):  File "<pyshell#8>", line 1, in <module>    'I love Python!' * 'love'TypeError: can't multiply sequence by non-int of type 'str'#字符串相减>>> 'I love Python!' - 'love'Traceback (most recent call last):  File "<pyshell#9>", line 1, in <module>    'I love Python!' - 'love'TypeError: unsupported operand type(s) for -: 'str' and 'str'#字符串相除>>> 'I love Python!' / 3Traceback (most recent call last):  File "<pyshell#10>", line 1, in <module>    'I love Python!' / 3TypeError: unsupported operand type(s) for /: 'str' and 'int'

更多细节请直接查看博文
http://www.cnblogs.com/yyhh/p/4202829.html?utm_source=tuicool&utm_medium=referral

http://www.codingpy.com/article/why-print-became-a-function-in-python-3/

0 0