Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

字符串与非字符串之间连接有几种方式 #6

Open
Sogrey opened this issue Feb 14, 2020 · 1 comment
Open

字符串与非字符串之间连接有几种方式 #6

Sogrey opened this issue Feb 14, 2020 · 1 comment

Comments

@Sogrey
Copy link
Owner

Sogrey commented Feb 14, 2020

1:加号

n = 20
s = s1 + str(n)
print(s)
v = 12.44
b = True
print(s1 + str(n) + str(v) + str(b))

hello20
hello2012.44True

2: 格式化

s = '<%s> <%d> <%.2f>' %(s1,n,v)
print('格式化:',s)

格式化: <hello> <20> <12.44>

3:重定向

from io import StringIO
import sys
old_stdout = sys.stdout
result = StringIO()
sys.stdout = result
print(s1, True, n,v,sep='*') #设置分隔符为 *
sys.stdout =old_stdout  # 恢复标准输出
result_str = result.getvalue()
print("用逗号连接:",result_str)

用逗号连接: hello*True*20*12.44

@Sogrey Sogrey added this to Python 基础 in Python QAs Feb 14, 2020
@Sogrey
Copy link
Owner Author

Sogrey commented Feb 14, 2020

#5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Python QAs
Python 基础
Development

No branches or pull requests

1 participant