0
点赞
收藏
分享

微信扫一扫

Python字符串拼接方法

罗蓁蓁 2022-01-05 阅读 176
def str_pinjie():
    #1、使用 + 号连接字符串
    str1 = "Hello"
    str2 = "Python3"
    res1 = str1 + str2
    print("使用 + 号连接字符串:", res1)

    #2、使用join方法
    list1 = ['你', '好', 'Python3']
    res2 = ''.join(list1)
    print("使用join方法连接字符串:", res2)
    #3、使用逗号拼接
    str3, str4 = 'Hello', 'word'
    res3 = str3, str4
    print(str3, str4)
    print("使用逗号拼接:", res3)
    print(type(res3))

    #4、直接拼接
    print('hello''python')

    #5、使用format拼接
    res4 = 'hello, word! {} {}'.format('张三', '李四')
    print("使用format拼接:", res4)
if __name__ == '__main__':
    str_pinjie()

输出结果:

使用 + 号连接字符串: HelloPython3
使用join方法连接字符串: 你好Python3
Hello word
使用逗号拼接: ('Hello', 'word')
<class 'tuple'>
hellopython
使用format拼接: hello, word! 张三 李四
举报

相关推荐

0 条评论