ezeke 发表于 2018-8-10 06:28:42

python入门(二)字符串的处理

  python常见的数据类型
  a= 10                     整数型
  b=10.0                     浮点型
  c=“hello”                  字符串
  d=True                     布尔   True/Fales
  应用方法
  整数:
  a=1
  print (a)
  浮点数:
  b=2.0
  print (b)
  布尔:
  print (b>a)
  True
  print   (b<a)
  Fales
  字符串:
  c=&quot;hello,tom&quot;
  查找:
  print c.find('h')
  返回下标
  0
  print c.find('o')
  4
  print c.find('w')
  查找不到返回
  -1
  替换
  print c.replace('tom','lili')
  hello,lili
  分割
  print c.split (',');
  以逗号问分隔符
  &quot;hello&quot;,&quot;tom&quot;
  集成整合
  c=&quot;:&quot;
  d=(&quot;hello&quot;,&quot;tom&quot;)
  print c.join(d);
  hello:tom
  前后去空格
  c=&quot;      hello,tom   &quot;
  print c.strip();
  hello,tom
  字符串格式化
  c=“hello,tom”
  {0}.format(c)
  hello,tom
  {0}{1}.format(&quot;hello&quot;,&quot;tom&quot;)
  hello tom
  {1}{0}.format(&quot;hello&quot;,&quot;tom&quot;)
  tom hello
  列表list[]
  列表可以把任何字符串,数字,字母加入,列表中的元素间没有任何的关联,列表冲存在下标,默认从0开始
  比如:
  conn=[&quot;1&quot;,&quot;1.11&quot;,&quot;hello&quot;,&quot;True&quot;]         这就是一个列表
  列表常用的处理方法:
  c=conn.append(&quot;lili&quot;)在列表最后添加一个元素
  print (c)
  1,1.11,hello,True,lili
  c=conn.pop()                     在列表最后删除一个元素
  print (c)
  1,1.11,hello
  conn.index()                     返回元素的下标
  print conn.index(&quot;hello&quot;)
  2
  conn.remove()                  根据下标删除元素
  同上
  conn.sort()                        排序
  conn.reverse()                  反向排序
  conn.[:]                           分片,前开后闭
  a=conn
  b=conn[:3]
  c=conn
  d=conn[:-1]
  print a
  
  print b
  
  print c
  
  print d
  
页: [1]
查看完整版本: python入门(二)字符串的处理