老齊python-基礎2(字符串)

一、字符串python

    1.1索引和切片api

        索引:編碼

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

        字符串中對應的索引:spa

         經過字符串找索引:code

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

         字符串切片:orm

>>> 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字符串基本操做blog

       len():求序列長度   #返回值爲一個整數索引

       +:鏈接2個序列ip

       *:重複序列元素utf-8

       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-八、gbk、gbk2312

      python3中默認就是utf8不須要聲明,python2中開頭聲明'# -*- coding: utf-8 -*-' 顯示中文才不會報錯

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

 

 

pass 55頁

相關文章
相關標籤/搜索