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

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

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

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

Sogrey opened this issue Feb 14, 2020 · 1 comment

Comments

@Sogrey
Copy link
Owner

Sogrey commented Feb 14, 2020

第1种:+(加号)

s1 = 'hello'
s2 = 'world'
s = s1 + s2
print(s)

helloworld

2: 直接连接

s = "hello""world"
print(s)

helloworld

3:用逗号(,)连接,标准输出的重定向

from io import StringIO
import sys
old_stdout = sys.stdout
result = StringIO()
sys.stdout = result
print('hello','world')
sys.stdout =old_stdout  # 恢复标准输出
result_str = result.getvalue()
print("用逗号连接:",result_str)

用逗号连接: hello world

4:格式化

s = '<%s> <%s>' % (s1,s2)
print('格式化:',s)

格式化: < hello > < world >

5: join

s = " ".join([s1,s2])
print("join连接:",s)

join连接: hello world

@Sogrey
Copy link
Owner Author

Sogrey commented Feb 14, 2020

#6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant