第八章[字符串]:8.6:字符串的连接

发布时间:2024年01月02日

一,字符串常量直接连接

1,字符串常量可以直接连接,
连接的两个字符串之间加不加空格均可

# 字符串常量可以直接连接,
# 两个字符串之间不加空格
s = 'hello'',world'
print(s)     # hello,world

# 两个字符串之间加空格
s = 'hello' ',world'
print(s)     # hello,world

运行结果:

hello,world
hello,world

2, 三个字符串的连接,中间加了空格

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

运行结果:

hello,  world

3,直接连接只能用于字符串常量,
不能用于字符串变量,否则会引起报错:

str1 = "hello,"
str2 = "world"
str3 = str1 str2

运行结果:

<
文章来源:https://blog.csdn.net/weixin_43881017/article/details/135333662
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。