zoukankan      html  css  js  c++  java
  • Python 拼接字符串的几种方式

    在学习Python(3x)的过程中,在拼接字符串的时候遇到了些问题,所以抽点时间整理一下Python 拼接字符串的几种方式。

    方式1,使用加号(+)连接,使用加号连接各个变量或者元素必须是字符串类型(<class 'str'>)

    例如:

    str_name1 = 'To'
    str_name2 = 'ny'
    str_name = str_name1 + str_name2
    print(str_name)

    输出结果:

    下面的代码会出现错误

    number=34
    print('这个数是:'+number)

    编译通过运行才发现行不通,出现了一下错误。

    修改Python 代码:

    number=34
    
    print('这个数是:'+str(number))

    方式三:使用.joiin(iterable) 拼接

    print('-----------method3-----------')
    # method3 使用join拼接字符串
    # str.join(iterable)
    # 可join的条件 join(iterable) iterable 可迭代的, 如果列表(list)为 非嵌套列表,列表元素为字符串(str)类型,
    # 序列类型,散列类型 都可以作为参数传入
    # eg(1):
    list_good_night = ['', '', '', '!']
    str_night = ''.join(list_good_night)
    print(str_night)
    # eg(2):
    # 拼接前缀 ('拼接前缀').join(iterable)
    str_night1 = '------>'.join(list_good_night)
    print(str_night1)
    # eg(3) 拼接 iterable = 字典 key,value 必须字符串 默认拼接key 的列表
    dict_name = {'key1': 'value1', 'key2': 'value2'}
    str_key = ','.join(dict_name)
    # 拼接value 的列表
    str_value = ','.join(dict_name.values())
    print(str_key)
    print(str_value)

    执行结果:

     方式四:使用逗号(,)拼接

    # method4 使用逗号(,)连接
    # 使用,逗号形式要注意一点,就是只能用于print打印,赋值操作会生成元组:
    print('-----------method4-----------')
    a, b = 'Hello', 'word'
    c = a, b
    print(a, b)
    print(c)
    print(type(c))

    输出结果:

     方式五:直接拼接

    # mehon5 直接连接
    print('-----------method5-----------')
     
    print('hello''python')

    方式六:format 拼接

    # mehon5 直接连接
    print('-----------method5-----------')
     
    print('hello''python')
     
    # methon6 format 拼接 str.format(args,**kwargs)
    # eg(1) {} 充当占位符
    str_word = 'hello, word! {} {}'.format('张三', '李四')
    print(str_word)
    # eg(2) {[index]} 按索引位置填充 .format([0]=value1, [1]= value1},)
    str_word_index0 = 'hell0, word!{0},{1}'.format('张三', '李四')
    str_word_index1 = 'hell0, word!{1},{0}'.format('张三', '李四')
    print(str_word_index0)
    print(str_word_index1)
    # eg(3) {[keyword]}
    str_word_keyword = 'hell0, word!{a},{b}'.format(b='张三', a='李四')
    print(str_word_keyword)
    # eg(4) {[keyword,indec]} keyword 放在最后
    str_word1 = 'hell0, word!{1}{a}{0},{b}'.format('index0', 'index1', b='张三', a='李四')
    print(str_word1)
    # eg(5) format 参数类型不限,当为元祖,列表,集合,字典时输出
    str_word2 = 'hell0, word!{b}'.format(b=['eee', 'd'])
    print(str_word2)
    # eg(6) 作为函数使用
    str_word3 = 'hello, word! {} {}'.format
    word = str_word3('张三', '李四')
    print(word)

    输出结果:

  • 相关阅读:
    [Hadoop in China 2011] 海狗不是狗 探秘支付宝准实时搜索查询
    [Hadoop in China 2011] 邵铮:揭秘FaceBook Puma演变及发展
    MongoDB学习笔记(一) MongoDB介绍及安装
    [Hadoop in China 2011] 人人网:基于Hadoop的SNS统计和聚类推荐
    运行中hadoop增加和删除datanode (*)
    第三届云计算大会 罗志国:中国移动大云的研发和实践(转载)
    [Hadoop in China 2011] 何鹏:Hadoop在海量网页搜索中应用分析
    Run hadoop example
    MongoDB学习笔记(三) 在MVC模式下通过Jqgrid表格操作MongoDB数据
    [Hadoop in China 2011] 蒋建平:探秘基于Hadoop的华为共有云
  • 原文地址:https://www.cnblogs.com/jiangxiaobo/p/11621813.html
Copyright © 2011-2022 走看看