1、直接通过+操作:

s = 'Python'+','+'你好'+'!'
print(s)

打印结果:

Python,你好!

2、通过join()方法拼接:

将列表转换成字符串

strlist=['Python', ',', '你好', '!']
print(''.join(strlist))

打印结果:

Python,你好!

3、通过format()方法拼接:

字符串中{}的数量要与format()方法中的参数数量一致

s = '{},{}!'.format('Python', '你好')
print(s)

打印结果:

Python,你好!

4、通过%拼接:

s = '%s,%s!' % ('Python', '你好')
print(s)

打印结果:

Python,你好!

5、通过()多行拼接:

当Python遇到未闭合的小括号,会自动将多行拼接成一行

s = (
'Python'
','
'你好'
'!'
)
print(s)

打印结果:

Python,你好!

6、通过string模块中的Template对象拼接:

from string import Template

s = Template('${s1},${s2}!')
# Template的实现方式是首先通过Template初始化一个字符串
# 这些字符串中包含了一个个key
print(s.safe_substitute(s1='Python', s2='你好'))
# 通过调用substitute或safe_subsititute
# 将key值与方法中传递过来的参数对应上
# 从而实现在指定的位置导入字符串

打印结果:

Python,你好!

7、通过F-strings(字符串插值)拼接:

s1 = 'Python'
s2 = '你好'
print(f'{s1},{s2}!')

打印结果:

Python,你好!

Python拼接字符串的7种方法的更多相关文章

  1. Python拼接字符串的七种方式

    忘了在哪看到一位编程大牛调侃,他说程序员每天就做两件事,其中之一就是处理字符串.相信不少同学会有同感. 几乎任何一种编程语言,都把字符串列为最基础和不可或缺的数据类型.而拼接字符串是必备的一种技能.今 ...

  2. Python 拼接字符串的几种方式

    在学习Python(3x)的过程中,在拼接字符串的时候遇到了些问题,所以抽点时间整理一下Python 拼接字符串的几种方式. 方式1,使用加号(+)连接,使用加号连接各个变量或者元素必须是字符串类型( ...

  3. Golang拼接字符串的5种方法及其效率_Chrispink-CSDN博客_golang 字符串拼接效率 https://blog.csdn.net/m0_37422289/article/details/103362740

    Different ways to concatenate two strings in Golang - GeeksforGeeks https://www.geeksforgeeks.org/di ...

  4. python3 拼接字符串的7种方法

    1.直接通过(+)操作符拼接 1 2 >>> 'Hello' + ' ' + 'World' + '!' 'Hello World!' 使用这种方式进行字符串连接的操作效率低下,因为 ...

  5. python连接字符串的几种方法--转子(香草拿铁的园子)

    一. str1+str2 string类型 ‘+’号连接 >>> str1="Good" >>> str2="Luck" & ...

  6. python 添加字符串的七种方法

    #使用{}的方法 s1 = 'Hello {}! My name is {}.'.format('World', 'Python猫') print(s1) s2 = 'Hello {0} My nam ...

  7. python 格式化字符串的三种方法

    1)%格式化方法 >>> a = "this is %s %s" % ("my", "apple") >>&g ...

  8. python拼接字符串方法汇总

    python拼接字符串一般有以下几种方法: 1.直接通过(+)操作符拼接 s = 'Hello'+' '+'World'+'!' print(s) 输出结果:Hello World! 这种方式最常用. ...

  9. Python 3 格式化字符串的几种方法!

    Python 3 格式化字符串的几种方法! %s和%d,%s是用来给字符串占位置,%d是给数字占位置,简单解释下: a = 'this is %s %s' % ('an','apple') 程序输出的 ...

随机推荐

  1. centos7 python3.5安装mysqlclient1.3.9

    MySQL-python目前不支持python3.5,可以使用mysqlclient 下载地址:https://pypi.python.org/pypi/mysqlclient/1.3.9 解压后进入 ...

  2. Java中线程同步的理解

    我们可以在计算机上运行各种计算机软件程序.每一个运行的程序可能包括多个独立运行的线程(Thread). 线程(Thread)是一份独立运行的程序,有自己专用的运行栈.线程有可能和其他线程共享一些资源, ...

  3. TCP-IP Architecture and IP Packet

    Why Internet working? To build a "network of networks" or internet. operating over multipl ...

  4. FreeRTOS 查询任务 剩余的栈空间的 方法

    FreeRTOS 源码下载地址 1.官方文档提供了   函数  用来查询  任务 剩余   栈 空间,首先是看官方的文档解释(某位大神 翻译 的 官方文档.) 参数解释:     xTask:被查询任 ...

  5. Advanced Plugin Concepts

    Provide Public Access to Default Plugin Settings An improvement we can, and should, make to the code ...

  6. 图形解析理解 css3 之倾斜属性skew()

    1.作用 改变元素在页面中的形状2.语法 属性:transform 函数: 1.skew(xdeg) 向横向倾斜指定度数 x取值为正,X轴不动,y轴逆时针倾斜一定角度 x取值为负,X轴不动,y轴顺时针 ...

  7. MongoTemplate复合条件查询

    分. 排序.按时间查询 Query query = new Query();        //必须条件        Criteria c = Criteria.where("VINID& ...

  8. python 创建虚拟环境

    创建一个文件夹:mkdir tf_env 进入到文件夹内:cd tf_env 创建虚拟环境:python3 -m venv tensorflow-dev 激活虚拟环境:source tensorflo ...

  9. youku客户端

    文件结构 config import os IP_PORT = ('127.0.0.1',8080) BASE_DIR = os.path.dirname(os.path.dirname(__file ...

  10. javascript 时间倒计时效果

    <div id="divdown1"></div> <script language="javascript" type=&quo ...