第一关:print()函数 - 入门到进阶(附练习题) | Python基础语法

在python里面,print()函数是我们最先接触到的,它是一个输出函数。你可以用它来输出你想要的东西。

下面小编就整理了在编程中,比较常用的print()函数入门到进阶的用法。

1.  print() 函数

无引号print(): 让计算机读懂括号里的内容,打印最终的结果。

单引号print(' '): 让计算机无需理解,原样打印引号中间的内容。

双引号print(" "): 让计算机无需理解,原样打印引号中间的内容。

三引号print(''' '''): 让计算机无需理解,原样打印引号中间的分段内容。

转义字符 \ +转义内容英文缩写首字母:

切记:符号和标点要使用英文输入法!

2.  print() 函数语法

print(*objects, sep=' ', end='\n', file=sys.stdout,flush=False)

参数的具体含义如下:

2.1  objects --表示输出的对象。输出多个对象时,需要用 , (逗号)分隔。

print('hello')
print('hello','world')
#hello
#hello world

2.2  sep -- 用来间隔多个对象。

print("www", "baidu", "com", sep=".")
#www.baidu.com

2.3  end -- 用来设定以什么结尾。默认值是换行符 \n,我们可以换成其他字符。

print('hello',end='~')
#hello~

2.4  file -- 要写入的文件对象。

f = open(r"test.txt","w")
# 打开test.txt文件,w为写入模式    
print('hello word',file = f)
# 输出hello word数据到test.txt文件中
f.close()
# 关闭文件

2.5  flush--参数用于控制输出缓存,一般为了可以获得较好的性能,保持为False即可,为True则刷新数据。

import time
f = open(r"test.txt","w")
# 打开test.txt文件,w为写入模式  
for i in range(1,6):    # 循环1-5
    # flush=True 此时终端数据会每秒打印数据 False则一起打印所有数据
    print(i,file = f,flush=True)    # 把1-5数据写入到文件
    time.sleep(1)   # 延迟一秒执行
    f1 = open(r"test.txt","r")  # 读取写入过数据的test.txt文件 
print(f1.read())    # 打印数据

3.  print() 函数变量的输出

无论什么类型的数据,包括但不局限于:数值型,布尔型,列表变量,字典变量...都可以直接输出。

num = 1
print(num) 
#1 输出数值型变量

str = 'Hello World'
print(str) 
# Hello World 输出字符串变量

list = [1,2,'a']
print(list) 
#[1, 2, 'a'] 输出列表变量

tuple = (1,2,'a')
print(tuple) 
#(1, 2, 'a') 输出元组变量

dict = {'a':1, 'b':2}
print(dict) 
# {'a': 1, 'b': 2} 输出字典变量

4.  数据的格式化输出

# 字符串格式化输出  %s
s = 'hi'
print('%s hello world' % s)
# hi hello world

# 整型数字格式化输出  %s
age = 18
print('我今年%d岁了' % age)
# 我今年18岁了

# 浮点型小数格式化输出  %f 默认保留6位四舍五入小数,%.3f代表保留3位小数
f = 3.1415926535
print('π约等于%f' % f)
# π约等于3.141593

5.  format用法

相对基本格式化输出采用‘%’的方法,format()功能更强大,该函数把字符串当成一个模板,通过传入的参数进行格式化,并且使用大括号‘{}’作为特殊字符代替‘%’

5.1  位置匹配

  (1)不带编号,即”{}”

  (2)带数字编号,可调换顺序,即”{1}”、”{2}”

  (3)带关键字,即”{a}”、”{b}”

print('{} {}'.format('hello','world'))  # 不带字段
# hello world
print('{0} {1}'.format('hello','world'))  # 带数字编号
# hello world
print('{0} {1} {0}'.format('hello','world'))  # 打乱顺序 0和1为 format的参数位置,该位置从索引0开始
# hello world hello
print('{2}, {1}, {0}'.format(*'abc'))  # 可打乱顺序
# 'c, b, a'
print('{a} {b} {a}'.format(b='hello',a='world'))  # 带关键字
# world hello world

5.2  进阶用法

左中右对齐及位数补全

(1)< (默认)左对齐、> 右对齐、^ 中间对齐、= (只用于数字)在小数点后进行补齐

(2)取位数“{:4s}”、"{:.2f}"等

print('{} and {}'.format('hello','world'))  # 默认左对齐
# hello and world
print('{:10s} and {:>10s}'.format('hello','world'))  # 取10位左对齐,取10位右对齐
# hello      and      world
print('{:^10s} and {:^10s}'.format('hello','world'))  # 取10位中间对齐
#   hello    and   world
print('{} is {:.2f}'.format(1.123,1.1234))  # 取2位小数
# 1.123 is 1.12
print('{0} is {0:>10.2f}'.format(1.123))  # 取2位小数,右对齐,取10位
# 1.123 is       1.12
print('{:<20}'.format('left align'))  # 左对齐
# left align
print('{:>20}'.format('right align'))  # 右对齐
#          right align
print('{:^20}'.format('centered'))  # 中间对齐
#      centered
print('{:*^20}'.format('center'))  # 使用*填充
# ******center******

练习题

同学们,先自觉练习,答案在公众号,公众号回复暗号【答案】即可。

1. 下列哪个选项可以打印出下列数据(多选):

# this's an apple.
# Yes.

A. print('this\'s an apple.\tYes.')
B. print("this\\'s an apple.Yes.")
C. print("this's an apple.\nYes.")
D. print('this\'s an apple.\nYes.')

2. 下列哪个选项可以打印出下列数据(单选):

# www.csdn.net-专业IT技术社区

A. print('www','csdn','net',sep='.',end='-专业IT技术社区')
B. print('www','csdn',sep='.','net',end='-专业IT技术社区')
C. print('www','csdn','net',end='-专业IT技术社区')

3. 下列哪个选项可以打印出下列数据(单选):

# 妈妈给了5岁的小明6.5块钱去买一瓶酱油。

A. print('%s给了%d岁的小明%.1f块钱去买一瓶酱油。' % ('妈妈',5,6.52))
B. print('%d给了%d岁的小明%.1f块钱去买一瓶酱油。' % ('妈妈',5,6.5))
C. print('%s给了%d岁的小明%f块钱去买一瓶酱油。' % ('妈妈',5,6.5))
D. print('%s给了%d岁的小明%.1f块钱去买一瓶酱油。'  ('妈妈',5,6.5))

4. 打印出以下"口"字形。(提示:使用< (默认)左对齐、> 右对齐、^ 中间对齐,以及*填充)

*******
*     *
*     *
*******

5. 打印99乘法表。(提示:先观察每一行数据变化的规律,每一行的数据都会多一项,并且数据依次递增)

1 * 1 = 1
1 * 2 = 2   2 * 2 = 4
1 * 3 = 3   2 * 3 = 6   3 * 3 = 9
1 * 4 = 4   2 * 4 = 8   3 * 4 = 12   4 * 4 = 16
1 * 5 = 5   2 * 5 = 10   3 * 5 = 15   4 * 5 = 20   5 * 5 = 25
1 * 6 = 6   2 * 6 = 12   3 * 6 = 18   4 * 6 = 24   5 * 6 = 30   6 * 6 = 36
1 * 7 = 7   2 * 7 = 14   3 * 7 = 21   4 * 7 = 28   5 * 7 = 35   6 * 7 = 42   7 * 7 = 49
1 * 8 = 8   2 * 8 = 16   3 * 8 = 24   4 * 8 = 32   5 * 8 = 40   6 * 8 = 48   7 * 8 = 56   8 * 8 = 64
1 * 9 = 9   2 * 9 = 18   3 * 9 = 27   4 * 9 = 36   5 * 9 = 45   6 * 9 = 54   7 * 9 = 63   8 * 9 = 72   9 * 9 = 81

每周每日,分享Python实战代码,入门资料,进阶资料,基础语法,爬虫,数据分析,web网站,机器学习,深度学习等等。


​微信群(关注「Python家庭」一起轻松学Python吧)

​QQ 群(983031854

发布了7 篇原创文章 · 获赞 44 · 访问量 8431

猜你喜欢

转载自blog.csdn.net/qq_34409973/article/details/104157807