函数print的各种用法

pyhton的基础函数print的用法,提醒pyhton3.0以上版本不支持没有()语句,必须要有括号,如以下语句无法运行。

print'hellow world'

特别提醒,print的end默认为换行,取消换行的方法如下

print("#####",end=" ")  #用空格代替换行

1.只打印数值或者某个变量时,直接打印便可。

i=5       #代表数值用例
print(i)
i="123456"    #字符串用例
print(i)
print(5)          #直接打印数字

2.打印字符串

print("hellow world")       #单行输出结果

print('nidihcfk\n'
      'jfgh\n'
      'dhuhdkjdh')  #多行输出结果
  
print('''      ni hao shijie
xiwan ni yi zhi hao
    wo ai zhong guo''')      #多行输出结果,注意包含空格。
    
#下面的输出会比上一个输出多两个换行,分别在开头和结尾。
print('''
      ni hao shijie
xiwan ni yi zhi hao
    wo ai zhong guo
    ''')            #多行输出结果,注意包含空格。

3.打印字符串和数值

print("nihao"+str(4)+"world")    #str()将数值转为字符

i=123446
print("nihao{:.2f}".format(i))      #使用format()函数实现

print("%s is open %s"%(i,j))    #自己看懂

print("###nihao",end='3456\n')       #使用end

4.打印列表,字典,集合类型

a=[1,2,3,4,5,6,7]   #列表

print(a)      #输出列表类型

print(str(a)+"nihaoshijie")   #打印列表加字符

新手易错,以下为错误代码

print(\n)

print(nihaoshijie)

总结,如果运行出现如下甚至其他类型不匹配的错误。

TypeError: can only concatenate list (not "str") to list

NameError: name 'nihaoshijie' is not defined

解决方法:将非字符类型转为字符类型,用字符类型输出结果,记住这方法,能帮你很大忙的。
当中出现了一些基础函数的简单调用,我们只需了解和使用,便足以满足我们期望的输出要求。
这是本人入门python时网上总结的输出形式,后续如有发现好的用法还会继续补充,欢迎各位大佬给我提建议和方法,我会认真学习和补充的,还望大佬大佬带带萌新。


版权声明:本文为confusingggg原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。