鱼C论坛

 找回密码
 立即注册
查看: 1611|回复: 2

[已解决]Python零基础变量和字符串下,动手题第二个

[复制链接]
发表于 2021-1-24 20:43:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
>>> for i in range(1, 10):
...     for j in range(1, i+1):
...         print(i, "x", j, "=", i * j, end=' ')
...     print("\n")
这是什么意思啊,到end看不懂了
最佳答案
2021-1-24 20:50:44

end 是 print 函数的一个默认参数,可以用 help 来看默认文档:
  1. >>>help(print)
  2. Help on built-in function print in module builtins:
  3. print(...)
  4.     print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
  5.    
  6.     Prints the values to a stream, or to sys.stdout by default.
  7.     Optional keyword arguments:
  8.     file:  a file-like object (stream); defaults to the current sys.stdout.
  9.     sep:   string inserted between values, default a space.
  10.     end:   string appended after the last value, default a newline.
  11.     flush: whether to forcibly flush the stream.
复制代码


可以看出,print 中的 end 参数默认值是为 '\n' 即换行符,这也是 print 函数为什么能够自动换行的原因

所以你重新赋值:print(i, "x", j, "=", i * j, end=' ')  就相当于将 end 的值改变

而 end 参数的作用是 print 函数内所有参数打印结束后,追加打印一个 end ,所以此时你的代码就默认追加打印一个 ' ' 空格字符串
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-1-24 20:50:44 | 显示全部楼层    本楼为最佳答案   

end 是 print 函数的一个默认参数,可以用 help 来看默认文档:
  1. >>>help(print)
  2. Help on built-in function print in module builtins:
  3. print(...)
  4.     print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
  5.    
  6.     Prints the values to a stream, or to sys.stdout by default.
  7.     Optional keyword arguments:
  8.     file:  a file-like object (stream); defaults to the current sys.stdout.
  9.     sep:   string inserted between values, default a space.
  10.     end:   string appended after the last value, default a newline.
  11.     flush: whether to forcibly flush the stream.
复制代码


可以看出,print 中的 end 参数默认值是为 '\n' 即换行符,这也是 print 函数为什么能够自动换行的原因

所以你重新赋值:print(i, "x", j, "=", i * j, end=' ')  就相当于将 end 的值改变

而 end 参数的作用是 print 函数内所有参数打印结束后,追加打印一个 end ,所以此时你的代码就默认追加打印一个 ' ' 空格字符串
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-24 21:06:43 | 显示全部楼层
        end 是 print() 函数的一个命名可选参数,end = '' 的作用是,改变 print() 函数在输出完信息后,会自动回车换行的行为,因为,如果不定义此参数,缺省值是 end = '\n',就是说,在显示完所有输入参数的内容后,会自动换行。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 08:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表