前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >10 - python print函数

10 - python print函数

原创
作者头像
ruochen
修改2021-05-24 10:26:40
8500
修改2021-05-24 10:26:40
举报

1. 使用print 函数输出字符串时,如何用逗号 (,) 分隔

代码语言:txt
复制
# 使用sep 参数设置字符串之间的分隔符,默认是空格

print('aa', 'bb')
# sep 可以用一个字符串作为分隔符
print('aa', 'bb', sep=',')
代码语言:txt
复制
aa bb
代码语言:txt
复制
aa,bb

2. 使用print 函数输出字符串时,如何不换行

代码语言:txt
复制
# 使用end 参数设置结尾符号,默认是换行符

print('hello')
print('world')

print('hello', end=' ')
print('world')
代码语言:txt
复制
hello
代码语言:txt
复制
world
代码语言:txt
复制
hello world

3. 如何用print 函数格式化输出

代码语言:txt
复制
# 可以使用 % 格式化字符串

s = 'road'
x = len(s)

print('The length of %s is %d' % (s, x))

from io import StringIO
import sys
old_stdout = sys.stdout
result = StringIO()
sys.stdout = result
print('The length of %s is %d' % (s, x))
sys.stdout = old_stdout
result_str = result.getvalue()
print('result_str', result_str, sep=': ')
代码语言:txt
复制
The length of road is 4
代码语言:txt
复制
result_str: The length of road is 4

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 使用print 函数输出字符串时,如何用逗号 (,) 分隔
  • 2. 使用print 函数输出字符串时,如何不换行
  • 3. 如何用print 函数格式化输出
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com