python3 基本数据类型   
Python3 中的变量不需要声明。每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建。
Python3 中有6个标准的数据类型:Number(数字);字符串(String);列表(list);元组(Tuple);字典:(Dict);集合(Sets) Number(数字):
Python3支持int,float,bool,complex(复数)
type()函数可以查看变量所指的对象类型 String(字符串):
Python中的字符串用单引号(')或双引号(")括起来,同时使用反斜杠(\)转义特殊字符。
注意:'''...'''三元引号在创建短字符串时没有什么特别用处,主要用于创建多行字符串,如下例:
>>> poem =  '''There was a Young Lady of Norway,
... Who casually sat in a doorway;
... When the door squeezed her flat,
... She exclaimed, "What of that?"
... This courageous Young Lady of Norway.''' Python也允许空串存在,不包含任何字符,完全合法!
数字与字符串之间的转换:

字符串转换成数字:
>>> int('20')
20
>>> float('20')
20.0
>>> int(20)
20 数字转换成字符串:
>>> str(20)
'20'
>>> str(20.0)
'20.0'
>>> str(True)
'True' 使用+拼接
在python中可以试用+将多个字符串或字符串变量进行拼接
>>> 'Release the Tom!' + 'At once!'
Release the Tom! At once! 使用[]提取字符
在字符串后面添加[],在[]中添加偏移量,即可取出该位置的字符串。第一个字符偏移量为0,第二个为1,后面依次类推。 右边第一个偏移量为-1,第二个为-2,从右往左依次类推...
>>> str = 'abcdefghijklmnopqrstuvwxyz'
>>> str[0]
'a'
>>> str[-1]
'z'
>>> str[3]
'd' 字符串是不可变的,有时候改变字符串,需要组合使用一些字符串函数:replace(),以及分片操作
>>> name = 'Henny'
>>> name.replace('H','P')
'Penny'
>>> 'P' + name[1:]
'Penny' 使用[start:end:step]分片:
分片操作:可以从一个字符串中抽取子字符串。起始偏移量start,终止偏移量end以及可选的步长step来定义一个分片
1.[:]提取从开头到结尾的整个字符串
2.[start:]从start提取到结尾
3.[:end]从开头提取到end-1
4.[start:end]从start提取到end-1结尾
5.[start:end:step]从start提取到end-1,每个step提取一个字符 >>> str = 'qwertyuiop'
>>> str[:]
'qwertyuiop'
>>> str[5:]
'yuiop'
>>> str[-3:]
'iop'
>>> str[:-3]
'qwertyu'
>>> str[-6:-3]
'tyu'
步长为3
>>> str[::3]
'qrup'
利用切片反向输出字符串
>>> str[::-1]
'poiuytrewq' 字符串其他常用操作:
>>> str = 'qwertyuiop' 计算字符串的长度
>>> len(str)
10 使用split()分割:
使用内置的字符串函数 split() 可以基于 分隔符 将字符串分割成由若干子串组成的 列表 。
所谓列表(list)是由一系列值组成的序列,值与值之间由逗号隔开,整个列表被方括号所包裹。
>>> todos = 'get gloves,get mask,give cat vitamins,call ambulance'
>>> todos.split(',')
['get gloves', 'get mask', 'give cat vitamins', 'call ambulance']
上面例子中,字符串名为 todos,函数名为 split(),传入的参数为单一的分隔符split(),传入的参数为单一的分隔符 ','。
如果不指定分隔符,那么 split() 将默认使用空白字符——换行符、空格、制表符。
>>> todos.split()
['get', 'gloves,get', 'mask,give', 'cat', 'vitamins,call', 'ambulance'] 使用join()合并:
可能你已经猜到了,join() 函数与 split() 函数正好相反:它将包含若干子串的列表分解,并将这些子串合成一个完整的大的字符串。join()
>>> crypto_list = ['Yeti', 'Bigfoot', 'Loch Ness Monster']
>>> crypto_string = ', '.join(crypto_list)
>>> print('Found and signing book deals:', crypto_string)
Found and signing book deals: Yeti, Bigfoot, Loch Ness Monster 使用replace()替换:
>>> str = 'qwertyuiop'
>>> str.replace('w','ooooo')
'qoooooertyuiop'
最多修改3处
>>> str = 'qwerwerwerwtytewwiitw'
>>> str.replace('w','oooo',3)
'qooooerooooerooooerwtytewwiitw'
计算字符串中'w'出现的次数
>>> str.count('w')
7

最新文章

  1. SOA、ESB、NServiceBus、云计算 总结
  2. sass心得
  3. 分离式模型separation model
  4. 跟着百度学PHP[4]OOP面对对象编程-15-魔术方法__call方法
  5. 受限玻尔兹曼机(RBM)学习笔记(一)预备知识
  6. iOS -- 生成有logo的二维码
  7. [Audio processing] FFMPEG转音频格式和采样率
  8. 马士兵 Servlet_JSP(2) JSP源代码)
  9. JavaScript学习总结(二)
  10. jQuery validate (转载)
  11. Sqlite出现SQL error: database disk image is malformed的处理
  12. unity3D 知识点随手记
  13. 南阳OJ-14-会场安排问题---区间不相交
  14. Java 图形化界面设计(GUI)实战练习(代码)
  15. 基于MongoDB2.6版本配置MongoDB主从复制集群架构
  16. solr基础使用概述
  17. jsp实现查询与展示功能
  18. Linux命令详解----ln
  19. 与Xamarin.Forms跨平台的用户界面
  20. bzoj 2784 时间流逝 —— 树上高斯消元

热门文章

  1. Linux 磁盘自动挂载
  2. POJ - 2240 Arbitrage(Bellman-Ford)
  3. 干货:制作科研slide简明规范
  4. webstorm去掉vue错误提示
  5. kombu源码Producer收获一
  6. UVALive 7456 Least Crucial Node
  7. 数链剖分(Tree)
  8. python-super1
  9. 【干货】操纵时间 感受威胁 MAC time时间戳视角
  10. Socket心跳包机制总结【转】