1、字符串

1.1索引和切片

索引:

  1. >>> lang = "study python"
  2. >>> lang[0]
  3. 's'
  4. >>> lang[1]
  5. 't'
    >>> "study python"[0]
    's'

字符串中对应的索引:

通过字符串找索引:

  1. >>> lang.index("p")
  2. 6

字符串切片:

  1. >>> lang = "study python"
  2. >>> lang[2:9]
  3. 'udy pyt'
  1. >>> lang = 'study python'
  2. >>> b = lang[1:] #得到从1号到末尾的字符,这时最后那个序号不用写
  3. >>> b
  4. 'tudy python'
  5. >>> c = lang[:] #得到所有的字符
  6. >>> c
  7. 'study python'
  8. >>> d = lang[:10] #得到从第一个到10号之前的字符
  9. >>> d
  10. 'study pyth'
  1. >>> e = lang[0:10]
  2. >>> e
  3. 'study pyth'
  4. >>> lang[1:11] #如果冒号后面有数字,所得到的切片不包含数字所对应的序号(前包括,后不包括)
  5. 'tudy pytho'
  6. >>> lang[1:]
  7. 'tudy python'
  1. >>> lang[1:12]
  2. 'tudy python'
  3. >>> lang[1:13]
  4. 'tudy python'

1.2字符串基本操作

len():求序列长度   #返回值为一个整数

+:连接2个序列

*:重复序列元素

in:判断元素是否存在于序列中

max():返回最大值

min():返回最小值

+:

  1. >>> str1 = 'python'
  2. >>> str2 = 'lisp'
  3. >>> str1 + str2 #字符串连接
  4. 'pythonlisp'
  5. >>> str1 + "&" + str2
  6. 'python&lisp'

in:

  1. >>> str1 = "python"
  2. >>> str2 = "lisp"
  3. >>> "p" in str1 #判断某个字符传是不是在另外一个字符串内,包含返回True 返回False
  4. True
  5. >>> "th" in str1
  6. True
  7. >>> "l" in str2
  8. True
  9. >>> "l" in str1
  10. False

max、min、ord、chr:

  1. >>> max(str1) #最值比较,按照ASCLL码
  2. 'y'
  3. >>> min(str1)
  4. 'h'
  1. >>> ord("y") #查看ASCLL码对应的顺序
  2. 121
  3. >>> ord("h")
  4. 104
  5. >>> chr(104) #通过ASCLL码顺对应顺序查找字符
  6. 'h'

字符串比较

  1. >>> 'a' > 'b'
  2. False
  3. >>> 'a' < 'b'
  4. True
  5. >>> "abc" > "aaa" #按照顺序比较字符串 1如果相等对比2,直到对比出大小
  6. True
  7. >>> "abc" < "a c"
  8. Fals

重复字符

  1. >>> a * 3
  2. 'hellohellohello'
  3. >>> print("-" * 30)
  4. ------------------------------

1.3字符串格式化输出

老用法不提倡:

  1. >>> "I like %s" % "python"
  2. 'I like python'
  3. >>> "I like %s" % "Pascal"
  4. 'I like Pascal'

新用法提倡:

  1. >>> "I like {0} and {1}".format("python","cangloshi")
  2. 'I like python and cangloshi'
  3.  
  4. >>> "I like {0:10} and {1:>15}".format("python","canglaoshi")
  5. 'I like python and canglaoshi'
  6. #{0:10} 为python预留10个字符,{1:>15}右对齐预留15字符
  7.  
  8. >>> "I like {0:^10} and {1:^15}".format("python","canglaoshi")
  9. 'I like python and canglaoshi '
  10. #居中显示
  11.  
  12. >>> "I like {0:.2} and {1:^10.4}".format("python","canglaoshi")
  13. 'I like py and cang '
  14. #显示第一个元素的前连个字符,第二个元素占10个字符 居中显示前4个元素
  15.  
  16. >>> "She is {0:d} years old and the breast is {1:f}cm".format(28,90.143598)
  17. 'She is 28 years old and the breast is 90.143598cm'
  18. #数字操作
  19.  
  20. >>> "She is {0:4d} years old and the breast is {1:6.2f}cm".format(28,90.143598)
  21. 'She is 28 years old and the breast is 90.14cm'
  22. #变量1占用4字节默认右对齐,变量2占用6字节右对齐,保留小数2位
  23.  
  24. >>> "She is {0:04d} years old and the breast is {1:06.2f}cm".format(28,90.143598)
  25. 'She is 0028 years old and the breast is 090.14cm'
  26. #位数不足用0补
  27.  
  28. >>> "I like {lang} and {name}".format(lang="python",name='canglaoshi')
  29. 'I like python and canglaoshi'
  30.  
  31. >>> data = {"name":"Canglaoshi","age":28}
  32. >>> "{name} is {age}".format(**data)
  33. 'Canglaoshi is 28'
  34. #字典用法

1.4常用字符串方法

  1. dir(str)
  2. #获取字符串所有方法
  3. help(str.isalpha)
  4. #多去方法帮助

1)判断是否全是字母

2)根据分隔符分割字符串

3)去掉字符串两头的空格

4)字符大小写转换

S.upper()     #S中的字母转换为大写
            S.lower()     #S中的字母转换为小写
            S.capitalize()   #将首字母转换为大写
            S.isupper()   #判断S中的字母是否全是大写
            S.islower()   #判断S中的字母是否全是小写
            S.istitle()   #判断S是否是标题模式,即字符串中所有的单词拼写首字母为大写,且其它字母为小写

5)用join拼接字符串

6)替换字符串

te = te.replace('test','OK')

  1. >>> "python".isalpha() #判断是否全是字母
  2. True
  3. >>> "python2".isalpha()
  4. False
  1. >>> a = "I LOVE PYTHON" #按照空格分割,生成列表
  2. >>> a.split(" ")
  3. ['I', 'LOVE', 'PYTHON']
  4. >>> b = "www.itdiffer.com"
  5. >>> b.split(".")
  6. ['www', 'itdiffer', 'com']
  1. >>> b = " hello "
  2. >>> b.strip() #去掉两边的空格
  3. 'hello'
  4. >>> b #未改变字符串本身
  5. ' hello '
    >>> b.lstrip() #去掉左边空格
    'hello '
    >>> b.rstrip() #去掉右边空格
    ' hello
  1. >>> a = "TAJZHANG"
  2. >>> a.istitle() #判断大写,返回布尔值
  3. False
  4. >>> a = "tAJZHANG"
  5. >>> a.istitle()
  6. False
  7. >>> a = "Taj,Zhang"
  8. >>> a.istitle()
  9. True
  1. >>> a = "This is a Book"
  2. >>> a.istitle()
  3. False
  4. >>> b = a.title()
  5. >>> b
  6. 'This Is A Book'
  7. >>> b.istitle()
  8. True
    >>> a = "Tajzhang"
    >>> a.isupper()
    False
    >>> a.upper().isupper() #全大写判断
    True
    >>> a.islower()
    False
    >>> a.lower().islower() #全小写判断
    True
  1. >>> b = 'www.itdiffer.com'
  2. >>> c = b.split(".")
  3. >>> c
  4. ['www', 'itdiffer', 'com']
  5. >>> ".".join(c)
  6. 'www.itdiffer.com'
  7. >>> "*".join(c)
  8. 'www*itdiffer*com'

1.5字符编码

计算机中的编码:ASCLL、Unicode、UTF-8、gbk、gbk2312

python3中默认就是utf8不需要声明,python2中开头声明'# -*- coding: utf-8 -*-' 显示中文才不会报错

  1. >>> import sys
  2. >>> sys.getdefaultencoding() #查看目前的编码
  3. 'utf-8'
  4. >>> ord("Q") #ASCLL码互转
  5. 81
  6. >>> chr(81)
  7. 'Q'

pass 55页

老齐python-基础2(字符串)的更多相关文章

  1. Python基础数据类型-字符串(string)

    Python基础数据类型-字符串(string) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客使用的是Python3.6版本,以及以后分享的每一篇都是Python3.x版 ...

  2. Python基础(二) —— 字符串、列表、字典等常用操作

    一.作用域 对于变量的作用域,执行声明并在内存中存在,该变量就可以在下面的代码中使用. 二.三元运算 result = 值1 if 条件 else 值2 如果条件为真:result = 值1如果条件为 ...

  3. python基础、字符串和if条件语句,while循环,跳出循环、结束循环

    一:Python基础 1.文件后缀名: .py 2.Python2中读中文要在文件头写: -*-coding:utf8-*- 3.input用法      n为变量,代指某一变化的值 n = inpu ...

  4. Python基础__字符串拼接、格式化输出与复制

    上一节介绍了序列的一些基本操作类型,这一节针对字符串的拼接.格式化输出以及复制的等做做详细介绍.一. 字符串的拼接 a = 'I', b = 'love', c = 'Python'. 我们的目的是: ...

  5. python基础类型—字符串

    字符串str 用引号引起开的就是字符串(单引号,双引号,多引号) 1.字符串的索引与切片. 索引即下标,就是字符串组成的元素从第一个开始,初始索引为0以此类推. a = 'ABCDEFGHIJK' p ...

  6. Python基础二字符串和变量

    了解一下Python中的字符串和变量,和Java,c还是有点区别的,别的不多说,上今天学习的代码 Python中没有自增自减这一项,在转义字符那一块,\n,\r\n都是表示回车,但是对于不同的操作系统 ...

  7. Python基础之字符串和编码

    字符串和编码 字符串也是一种数据类型,但是字符串比较特殊的是还有个编码问题. 因为计算机自能处理数字,如果徐娅处理文本,就必须先把文本转换为数字才能处理,最早的计算机子设计时候采用8个比特(bit)作 ...

  8. Python高手之路【六】python基础之字符串格式化

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...

  9. Python开发【第一篇】Python基础之字符串格式化

    字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-310 ...

随机推荐

  1. qt5.2.1在linux下去除最大化和最小化按钮

    #include <QtGui/QGuiApplication> #include <QDebug> #include <QScreen> #include &qu ...

  2. Chunky Monkey

    猴子吃香蕉可是掰成好几段来吃哦! 把一个数组arr按照指定的数组大小size分割成若干个数组块. 例如:chunk([1,2,3,4],2)=[[1,2],[3,4]]; chunk([1,2,3,4 ...

  3. vue 跨域

    注意!只能在本地调试使用,上线后url会出错使用以下方法要先引入网络模块 先配置文件:config =>index.js以下部分改为:proxyTable: { '/apis': { // 测试 ...

  4. lambda表达式 <二>

    概念了解: 1.什么是匿名委托(匿名方法的简单介绍.为什么要用匿名方法) 2.匿名方法的[拉姆达表达式]方法定义 3.匿名方法的调用(匿名方法的参数传递.使用过程中需要注意什么) 什么是匿名方法? 匿 ...

  5. 第6课:datetime模块、操作数据库、__name__、redis、mock接口

    1.  datetime模块 import datetime print(datetime.datetime.today()) # 当前时间 2018-01-23 17:22:35.739667 pr ...

  6. c# 验证码图片生成类

    using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D ...

  7. jQuery 滑动选项卡jQuery tabulous.js

    A jQuery tabs module for todays web! 实例DEMO 运行一下 Documentation Tabulous.js can be used with any cont ...

  8. 通过摄像机视频设备或者流媒体服务器SDK获取到数据转换成RTMP流实现网页/手机微信播放

    写这篇博客主要是为了给新入门的流媒体开发者解惑,现在看到各种开发者的需求:网页播放RTSP摄像机.微信播放RTSP摄像机.网页播放摄像机SDK输出的视频流.网页播放第三方流媒体平台输出的视频流.包括G ...

  9. KAFKA 0.11 RHEL6.5安装

    KAFKA简介 KAFKA是一款分布式消息发布和订阅的系统. 官网:http://kafka.apache.org/ 1.下载KAFKA及JDK KAFKA下载地址: http://kafka.apa ...

  10. [Python] print中的左右对齐问题

    一.数值类型(int.float) #  %d.%f是占位符>>> a = 3.1415926>>> print("%d"%a)    #%d只 ...