Python学习笔记-- 字符串和数字的连接
Python学习笔记– 字符串和数字的连接
>>> s = 'abc'
>>> print s + 1Traceback (most recent call last):File "<pyshell#4>", line 1, in <module>print s + 1
TypeError: cannot concatenate 'str' and 'int' objects
上面运算中提到出现了类型错误,这里不难看出是类型转换的问题对于类似问题,有以下几种解决办法:
- 通过str构造函数来实现
>>> s = 'abc'
>>> print s + str(1)
abc1
通过help(str),我们可以获得以下帮助信息(python2.7.9)
str(object=”) -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object.
我们不难看出,传入数字1到str()中,会返回1的字符串表现形式,即将数字1转换为string类型。
- 通过字符串的格式化
>>> s = 'abc'
>>> print "%s%s"%(s,1)
abc1
>>>
这里是因为%s就是通过str()来处理对象的(详见:python中%r和%s的区别)
- 通过print来实现
>>> s = 'abc'
>>> x = 1
>>> print x,s
1 abc
>>> print 1,s
1 abc
在print的帮助信息中有如下信息:
“print” evaluates each expression in turn and writes the resulting
object to standard output (see below). If an object is not a string,
it is first converted to a string using the rules for string
conversions.
意思就是说,当打印的对象不是string,会先进行向string类型的转换
>>> print 'abc' + 1Traceback (most recent call last):File "<pyshell#29>", line 1, in <module>print 'abc' + 1
TypeError: cannot concatenate 'str' and 'int' objects
>>>
“+”运算符有连接字符串的作用(在连接两个字符串是重载为字符串连接符),但是在连接字符串和数字时,会被认为是运算符加号,而在加法运算中,出现了类型不一致,所以报错。
引用博文
[1]:python中%r和%s的区别
http://blog.csdn.net/wusuopubupt/article/details/23678291
Python学习笔记-- 字符串和数字的连接相关推荐
- python学习笔记5-罗马数字转整数
python学习笔记5-罗马数字转整数 题目: 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 ...
- Python学习笔记——字符串、列表、元组、字典
字符串介绍 想一想: 当打来浏览器登录某些网站的时候,需要输入密码,浏览器把密码传送到服务器后,服务器会对密码进行验证,其验证过程是把之前保存的密码与本次传递过去的密码进行对比,如果相等,那么就认为密 ...
- Python学习笔记-字符串相关
字符串有 下标(索引) ,从0开始,到n-1结束 len() 获取字符串长度 s = 'Hello' len(s) 利用索引获取字符串中某一位 正数索引 从0开始,从左向右数 负数索引 从-1开始,从 ...
- Python学习笔记-字符串
目录 一.字符串类型 二.字符串的常用操作 1.字符串拼接 2.计算字符串的长度 3.截取字符串 4.分割.合并字符串 4.1 分格字符串 4.2 多字符分割字符串 4.3 合并字符串 5.检索字符串 ...
- Python 学习笔记 字符串的功能与方法
一.字符串基础功能: 1.什么是字符串: 简单讲用引号包裹的就是字符串,可以是单引号或双引号. 2.字符串的基础操作: 创建字符串: # 创建空字符串 s = '' # 创建非空字符串 s = 'he ...
- python学习笔记---字符串
字符串 常用的操作方法 .count() .find() .join() .encode() .decode() .endswith() .startswitch('A') .isdigit() .s ...
- python学习笔记——字符串操作
字符串相关操作 #!/usr/bin/env python3 # encoding: utf-8 # author: huangwj # mail: huangwjwork@gmail.com # b ...
- Python学习笔记字符串Str
python中的str到底是什么类型? https://docs.python.org/3/library/stdtypes.html#textseq 方法都有哪些? http://www.cnpyt ...
- Python学习笔记总结
了解了python语言后,决定以后使用python来进行各项工作,因此一直想要深入学习python.千里之行始于足下,万事开头难. 由于最近在准备写毕业论文,陆陆续续学习了Python的语法知识. P ...
最新文章
- AXI DMA DRIVER 阶段性 kernel driver 构建并测试(三 )
- synchronized原理_synchronized关键字的作用、原理以及锁优化
- centos7 postgresql安装
- 25 款软件上榜,2020“最佳开源奖” 出炉!
- 基于HT for Web的Web SCADA工控移动应用
- 在CentOs6.5安装jdk
- Python 3.x 中“HTTP Error 403: Forbidden“问题的解决方案
- pagerTabStrip例子
- mysql转储表_Mysql导出表结构及表数据 mysqldump用法
- 局域网arp攻击_图解ARP协议(二)ARP攻击原理与实践
- 在Linux上安装字体
- html游戏网站设计报告,web前端课程设计以及报告,jquery+js+css+html
- 太白---落燕纷飞第一重 Android单元测试Instrumentation和irobotium
- 3步实现 华为云服务器IPV4-> IPV6改造方法
- 骆昊python100天百度网盘_GitHub - yiailake/Python-100-Days: Python - 100天从新手到大师
- 《人生的智慧》-叔本华著[韦启昌-(译)]
- MySQL深入了解与性能优化
- Airbus和IBM计划让机器人CIMON加入国际空间站执行任务
- 华为C8812E刷为MIUI总结
- 虚拟逃避现实非常适合大流行,但虚拟现实似乎离主流技术还很远