• Python 拼接字符串的6种方法总结:


    拼接字符串的6种方法总结:

    1.自C语言的%方式

    print('第 %d 种:' % 1)
    url = 'this is a %s and %s'
    print(url % ('dog', 'cat'))
    

    2.format()拼接方式

    print('第 %d 种:' % 2)
    url = 'this is a {} and {}'
    print(url.format('dog', 'cat'))
    url = 'this is a {animal1} and {animal2}'
    print(url.format(animal1='dog', animal2='cat'))
    

    3.() 类似元组方式

    print('第 %d 种:' % 3)
    url = 'this is a dog and'
    print(url, 'cat')
    

    4.常用的+号方式,都必须是字符串类型

    print('第 %d 种:' % 4)
    url = 'this is a dog and '
    print(url + 'cat')
    

    5.join()拼接方式

    print('第 %d 种:' % 5)
    url_list = ['this is a dog ', ' cat']
    print('and'.join(url_list))
    

    6.f-string方式

    print('第 %d 种:' % 6)
    animal = 'cat'
    url = f'this is a dog and {animal}'
    print(url)
    

    结果:

    第 1 种:
    this is a dog and cat
    第 2 种:
    this is a dog and cat
    this is a dog and cat
    第 3 种:
    this is a dog and cat
    第 4 种:
    this is a dog and cat
    第 5 种:
    this is a dog and cat
    第 6 种:
    this is a dog and cat
    
    十万签名内容......
  • 相关阅读:
    【安装软件的点点滴滴】
    【自然语言处理】LDA
    【sklearn】数据预处理 sklearn.preprocessing
    【sklearn】中文文档
    【MySql】update用法
    DotNet Core
    ASP.NET MVC
    ADO.NET
    RESTful API
    C#
  • 原文地址:https://www.cnblogs.com/myswift/p/14926613.html
Copyright © 2020-2023  润新知