Python 拼接字符串的几种方式
在学习Python(3x)的过程中,在拼接字符串的时候遇到了些问题,所以抽点时间整理一下Python 拼接字符串的几种方式。
方式1,使用加号(+)连接,使用加号连接各个变量或者元素必须是字符串类型(<class 'str'>)
例如:
str_name1 = 'To'
str_name2 = 'ny'
str_name = str_name1 + str_name2
print(str_name)
输出结果:
下面的代码会出现错误
number=
print('这个数是:'+number)
编译通过运行才发现行不通,出现了一下错误。
修改Python 代码:
number= print('这个数是:'+str(number))
方式三:使用.joiin(iterable) 拼接
print('-----------method3-----------')
# method3 使用join拼接字符串
# str.join(iterable)
# 可join的条件 join(iterable) iterable 可迭代的, 如果列表(list)为 非嵌套列表,列表元素为字符串(str)类型,
# 序列类型,散列类型 都可以作为参数传入
# eg():
list_good_night = ['晚', '上', '好', '!']
str_night = ''.join(list_good_night)
print(str_night)
# eg():
# 拼接前缀 ('拼接前缀').join(iterable)
str_night1 = '------>'.join(list_good_night)
print(str_night1)
# eg() 拼接 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() {} 充当占位符
str_word = 'hello, word! {} {}'.format('张三', '李四')
print(str_word)
# eg() {[index]} 按索引位置填充 .format([]=value1, []= 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() {[keyword]}
str_word_keyword = 'hell0, word!{a},{b}'.format(b='张三', a='李四')
print(str_word_keyword)
# eg() {[keyword,indec]} keyword 放在最后
str_word1 = 'hell0, word!{1}{a}{0},{b}'.format('index0', 'index1', b='张三', a='李四')
print(str_word1)
# eg() format 参数类型不限,当为元祖,列表,集合,字典时输出
str_word2 = 'hell0, word!{b}'.format(b=['eee', 'd'])
print(str_word2)
# eg() 作为函数使用
str_word3 = 'hello, word! {} {}'.format
word = str_word3('张三', '李四')
print(word)
输出结果:
Python 拼接字符串的几种方式的更多相关文章
- Python拼接字符串的七种方式
忘了在哪看到一位编程大牛调侃,他说程序员每天就做两件事,其中之一就是处理字符串.相信不少同学会有同感. 几乎任何一种编程语言,都把字符串列为最基础和不可或缺的数据类型.而拼接字符串是必备的一种技能.今 ...
- C# 拼接字符串的几种方式和性能
开发过程中常用到的拼接字符串的方法有三种: 1 简单 “+=” 拼接法 1 2 3 4 5 string str="a"; str+="c"+"d ...
- Python拼接字符串的7种方法
1.直接通过+操作: s = 'Python'+','+'你好'+'!'print(s) 打印结果: Python,你好! 2.通过join()方法拼接: 将列表转换成字符串 strlist=['Py ...
- oracle 拼接字符串的两种方式
方式一:使用管道符||进行拼接 方式二:使用concat()函数 区别: 方式一可以拼接多个字符串:方式二只能将2个字符串拼接到一起. 写在最后 哪位大佬如若发现文章存在纰漏之处或需要补充更多内容 ...
- python中字符串的几种表达方式(用什么方式表示字符串)
说明: 今天在学习python的基础的内容,学习在python中如何操作字符串,在此记录下. 主要是python中字符串的几种表达,表示方式. python的几种表达方式 1 使用单引号扩起来字符串 ...
- python中字符串的四种表达方式
今天在学习python的基础的内容,学习在python中如何操作字符串,在此记录下. 主要是python中字符串的几种表达,表示方式. python的几种表达方式 1 使用单引号扩起来字符串 > ...
- python拼接字符串方法汇总
python拼接字符串一般有以下几种方法: 1.直接通过(+)操作符拼接 s = 'Hello'+' '+'World'+'!' print(s) 输出结果:Hello World! 这种方式最常用. ...
- python拼接字符串
python拼接字符串一般有以下几种方法: 1.直接通过(+)操作符拼接 s = 'Hello' + ' ' + 'World' + '!' print(s) 输出结果:Hello World! 使用 ...
- Python格式化输出的三种方式
Python格式化输出的三种方式 一.占位符 程序中经常会有这样场景:要求用户输入信息,然后打印成固定的格式比如要求用户输入用户名和年龄,然后打印如下格式:My name is xxx,my age ...
随机推荐
- sql 语句中关于 not in 和 null 的问题简单解析
理解这个问题,只需要记住一个逻辑: null 和任何值比较运算都返回的 false Ex: SQL01: SELECT * FROM userinfo WHERE age NOT IN() SQL01 ...
- JavaScript: 数据类型检测
由于JavaScript是门松散类型语言,定义变量时没有类型标识信息,并且在运行期可以动态更改其类型,所以一个变量的类型在运行期是不可预测的,因此,数据类型检测在开发当中就成为一个必须要了解和掌握的知 ...
- MyBatis 中如何调用 Java 的 enum (枚举) 字段
事先作成一 enum,如下: public enum CityCode { ALL("000"), BEIJING("010"), SHANGHAI(" ...
- 谈谈OAuth1,OAuth2异同
##一.写在前面在收集资料时,我查询和学习了许多介绍OAuth的文章,这些文章有好有坏,但大多是从个例出发.因此我想从官方文档出发,结合在stackoverflow上的一些讨论,一并整理一下.整理的内 ...
- nodejs express+nodemon 实现自动刷新 (热更新)
1.全局安装 nodemon npm i nodemon -g -f 2. 在项目中安装nodemon npm i nodemon -f 3.使用nodemon 在express项目中 运行nodem ...
- JQuery中两个ul标签的li互相移动
实例 <html > <head> <meta http-equiv="Content-Type" content="text/html; ...
- linux虚拟串口及远程访问
1. 虚拟终端概念 linux中有很多终端,如下简单介绍下各种终端或串口的概念. 1.1 tty:终端设备的统称 tty是Teletype或TeletypeWriter的缩写,中文翻译为电传打字机.电 ...
- HTML&CSS基础-外部样式表
HTML&CSS基础-外部样式表 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.如下图所示,有2个文件 p { color:pink; font-size:40px; ...
- Android init介绍(下)
上一篇请参考<Android init介绍(上)> 5. AIL 在init启动过程中,系统服务等均是通过解析rc文件来启动,而rc文件则是由Android初始化语言(Android In ...
- Mybatis分页方法
使用方法https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md 使用 Maven 在 pom ...