zoukankan      html  css  js  c++  java
  • 老齐python-基础2(字符串)

    1、字符串

        1.1索引和切片

            索引:

    >>> lang = "study python"
    >>> lang[0]
    's'
    >>> lang[1]
    't'
    >>> "study python"[0]
    's'

            字符串中对应的索引:

             通过字符串找索引:

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

             字符串切片:

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

       1.2字符串基本操作

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

           +:连接2个序列

           *:重复序列元素

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

           max():返回最大值

           min():返回最小值  

        +:

    >>> str1 = 'python'
    >>> str2 = 'lisp'
    >>> str1 + str2       #字符串连接
    'pythonlisp'
    >>> str1 + "&" + str2
    'python&lisp'

        in:

    >>> str1 = "python"
    >>> str2 = "lisp"
    >>> "p" in str1        #判断某个字符传是不是在另外一个字符串内,包含返回True 返回False
    True
    >>> "th" in str1
    True
    >>> "l" in str2
    True
    >>> "l" in str1
    False

        max、min、ord、chr:

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

     字符串比较

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

    重复字符

    >>> a * 3
    'hellohellohello'
    >>> print("-" * 30)
    ------------------------------

         1.3字符串格式化输出

            老用法不提倡:

    >>> "I like %s" % "python"
    'I like python'
    >>> "I like %s" % "Pascal"
    'I like Pascal'

            新用法提倡:

    >>> "I like {0} and {1}".format("python","cangloshi")
    'I like python and cangloshi'
    
    >>> "I like {0:10} and {1:>15}".format("python","canglaoshi")
    'I like python     and      canglaoshi'
    #{0:10} 为python预留10个字符,{1:>15}右对齐预留15字符
    
    >>> "I like {0:^10} and {1:^15}".format("python","canglaoshi")
    'I like   python   and   canglaoshi   '
    #居中显示
    
    >>> "I like {0:.2} and {1:^10.4}".format("python","canglaoshi")
    'I like py and    cang   '
    #显示第一个元素的前连个字符,第二个元素占10个字符 居中显示前4个元素
    
    >>> "She is {0:d} years old and the breast is {1:f}cm".format(28,90.143598)
    'She is 28 years old and the breast is 90.143598cm'
    #数字操作
    
    >>> "She is {0:4d} years old and the breast is {1:6.2f}cm".format(28,90.143598)
    'She is   28 years old and the breast is  90.14cm'
    #变量1占用4字节默认右对齐,变量2占用6字节右对齐,保留小数2位
    
    >>> "She is {0:04d} years old and the breast is {1:06.2f}cm".format(28,90.143598)
    'She is 0028 years old and the breast is 090.14cm'
    #位数不足用0补
    
    >>> "I like {lang} and {name}".format(lang="python",name='canglaoshi')
    'I like python and canglaoshi'
    
    >>> data = {"name":"Canglaoshi","age":28}
    >>> "{name} is {age}".format(**data)
    'Canglaoshi is 28'
    #字典用法

        1.4常用字符串方法

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

            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')

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

        1.5字符编码

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

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

    >>> import sys
    >>> sys.getdefaultencoding()  #查看目前的编码
    'utf-8'
    >>> ord("Q")   #ASCLL码互转
    81
    >>> chr(81) 
    'Q'

    pass 55页

  • 相关阅读:
    吴裕雄--天生自然 R语言开发学习:广义线性模型(续一)
    吴裕雄--天生自然 R语言开发学习:广义线性模型
    吴裕雄--天生自然 R语言开发学习:重抽样与自助法(续一)
    吴裕雄--天生自然 R语言开发学习:重抽样与自助法
    吴裕雄--天生自然 R语言开发学习:中级绘图(续二)
    吴裕雄--天生自然 R语言开发学习:中级绘图(续一)
    吴裕雄--天生自然 R语言开发学习:中级绘图
    吴裕雄--天生自然 R语言开发学习:功效分析(续一)
    吴裕雄--天生自然 R语言开发学习:功效分析
    吴裕雄--天生自然 R语言开发学习:方差分析(续二)
  • 原文地址:https://www.cnblogs.com/Taj-Zhang/p/7406876.html
Copyright © 2011-2022 走看看