字符串

字符串或串(String)是由数字、字母、下划线组成的一串字符,用双引号或单引号包裹的为字符串

1 >>> "hello world"
2 'hello world'
3 >>> 'hello world'
4 'hello world'
5 >>> "250"
6 '250'
7 >>> type("200")
8 <type 'str'>

下面示例:

  语法错误 第一行出现三个单引号,Python 解析器匹配不上成对的引号,所以报错。

  解决方法:1、可使用双引号包裹 2、可以使用反斜杠\ 转义字符

 1 >>> 'What's your name?'
2 File "<stdin>", line 1
3 'What's your name?'
4 ^
5 SyntaxError: invalid syntax
6 >>> "What's your name?"
7 "What's your name?"
8 >>> 'What\'s your name?'
9 "What's your name?"
10 >>>

字符串 、数字互转

  内置函数int()  str() float()

 1 >>> a = int("200")
2 >>> a
3 200
4 >>> type(a)
5 <type 'int'>
6 >>> b = str(200)
7 >>> type(b)
8 <type 'str'>
9 >>> c = float("200.5")
10 >>> type(c)
11 <type 'float'>
12 >>>

转义符

 第5行出现 \n 换行

 解决方法:使用反斜杠\ 或者在原始字符串前+r ,会显示原始字符串

1 >>> print "c:\\news"
2 c:\news
3 >>> print r"c:\news"
4 c:\news
5 >>> print "c:\news"
6 c:
7 ews

字符串相加

 字符串相加是将两个字符串拼接在一起

 1 >>> "3" + "6"
2 '36'
3 >>> "py" + "thon"
4 'python'
5 >>> 8 + "6"
6 Traceback (most recent call last):
7 File "<stdin>", line 1, in <module>
8 TypeError: unsupported operand type(s) for +: 'int' and 'str' 不支持 int 和字符串相加,我们可以将其转换
9 >>> 8 + int("6")
10 14
11 >>> str("8")+ "6"
12 '86'

最新文章

  1. 手机浏览器,微信中播放amr录音
  2. EWS API 2.0读取日历信息-读取内容注意事项
  3. Berkeley DB数据处理
  4. inheritance,菱形继承, 虚继承,virtual
  5. Linux下top订购具体解释
  6. 利用btrace工具监控在线运行java程序
  7. filezilla无法连接linux服务器
  8. BZOJ 3689 异或 Trie木+堆
  9. python zookeeper 在 uwsgi中 watcher不生效
  10. 一个好用的PHOTOSHOP切图插件(CutterMan插件下载)
  11. eclipse的常用快捷键和一些基本设置!!!
  12. C#嵌套类
  13. C# 链表去重 List 一维 二维 分别使用 Distinct() GroupBy() 方法
  14. git 安装配置
  15. Java的BIO,NIO和AIO的区别于演进
  16. Centos7上搭建ftp服务器
  17. Android Monkey的使用
  18. MySQL和ORACLE、SQL Server、PostgreSQL相比
  19. 【Pyton】【小甲鱼】魔法方法
  20. asp.net core 2.0 试用

热门文章

  1. mc:Ignorable=&quot;d&quot;什么意思?
  2. mysql的引擎和锁
  3. Spring课程 Spring入门篇 5-3 配置切入点 pointcut
  4. 从零开始的全栈工程师——html篇1.2
  5. axios 发 post 请求,后端接收不到参数的解决方案
  6. HTML字符实体名称/实体编号
  7. Angular选项卡
  8. javascript实现数据结构: 稀疏矩阵之三元组线性表表示
  9. ezdpl Linux自动化部署实战
  10. (转)防止ViewPager中的Fragment被销毁的方法