以字符串爲例,談談Python到底要學到什麼程度-Python基礎前傳(4)

古語云:慈不掌兵,義不爲商;離商業越近,離人性越遠;我們在自學數據科學時,一定會輔助一些書籍或者視頻來學習,怎麼學習,選擇哪些資料來學習?這時,我們都要理解好第一句話,理解不好,浪費錢是次要,重要的是還會浪費我們的時間;

對於學習數據科學這門技術來說,一切速成,都只是拔苗助長;數據分析不能速成,數據挖掘不能速成,7天,1個月,都不可能學到什麼,鑽研一門技術6個月,你也只能會有一點入門的感覺,而已;聽上去有點荒涼是吧,可忠言逆耳;

Python僅僅是一門編程語言,以後小學都普及了,現在卻被培訓機構捧上了天;欲讓其滅亡,必先讓其膨脹,對於Python來說,這並不是什麼好事,對於學習Python的人來說,這更不是什麼好事,學習技術跟學武一樣,要內外兼修,內功紮實才能厚積薄發;可這個浮躁的社會,要做到這點耐得住寂寞的修煉內功,不容易;

那麼問題拋出來了,我們該如何學習一門技能呢,我拿Python學習舉例,說說我的看法;

學習沒有捷徑,但一定有方法,有很多朋友問我:Python到底怎麼學,要學到什麼程度,在工作中才夠用?今天,jacky以字符串的學習爲例,談談Python我們到底要學到什麼程度,要怎麼學?

(一)入門水平

1.1入門水平我們能做什麼?

  • 很遺憾的說,對於零基礎的朋友來說,入門水平,只能提高我們學習Python的興趣,僅此而已; 

    • 零基礎學Python的朋友,由於缺少專業背景和實操背景,僅靠短時間的自學,大部分都也只是入門水平,大家還要繼續學習,不要學到這裏就停下了,堅持學習,才能看到陽光;

1.2 掌握哪些知識點纔算入門

  • 1) 知道字符串是幹什麼的?

    • 用於在程序中顯示消息的(比如遊戲裏的”準備”和”遊戲結束”這樣的消息)

  • 2)知道如何創建字符串

    • 知道單引號,雙引號,三引號的區別;

    • 英文半角引號

  • 3)知道如何在字符串裏嵌入值

    • 佔位符%s

>>>myscore = 100
>>>message='I scored %s points'
>>>print(message % myscore) I scored 100 points
  • 4)知道字符串乘法

>>>print(10*'a')
aaaaaaaaaa

(二)初級水平

2.1 初級水平我們能做什麼?

  • 做一些數據分析類的輔助工作,足夠了,但是對於數據工程師或挖掘師來說,差的還太遠;

    • 學到初級水平,我們有時會自我膨脹,對於外行人可能還會炫耀一下,但是對於有經驗的老手來說,初級水平離實操還有很長的一段差距要走;

2.2 掌握哪些知識點纔算初級水平

  • 掌握如下字符串功能(具體講解見中級水平部分)

    • 移除空白

    • 分割

    • 長度

    • 索引

    • 切片

(三)中級水平

3.1 中級水平我們能做什麼?

  • 中級水平,我們只能跟別人說,Python我學過,關於獨當一面的工作,這個階段還是別想;學到中級水平我們纔有可能知道,原來我們要學的東西太多了,而且在工作中都能用的上,學習中的所有技巧,對於工作來說,都是最重要的技巧;積累很重要,學無止境;

3.2 中級水平我們需掌握字符串的所有功能

temp = 'jacky'help(type(temp))

功能1:capitalize(首字母大寫)

def capitalize(self):  # real signature unknown; restored from __doc__
        """
        S.capitalize() -> str

        Return a capitalized version of S, i.e. make the first character
        have upper case and the rest lower case.
        """
        return
    # 首字母大寫
    # >>> a = "abel"
    # >>> b = a.capitalize()
    # >>> b
    # 'Abel'

功能2:center(內容居中空白填充)

    def center(self, width, fillchar=None):  # real signature unknown; restored from __doc__
        """
        S.center(width[, fillchar]) -> str

        Return S centered in a string of length width. Padding is
        done using the specified fill character (default is a space)
        """
        return ""
    # 內容居中,width: 總長度; fillchar:空白處填充內容,默認無
    # >>> a
    # 'Abel'
    # >>> b = a.center(20, '#')
    # >>> b
    # '########Abel########'

功能3:count(子序列個數)

    def count(self, sub, start=None, end=None):  # real signature unknown; restored from __doc__
        """
        S.count(sub[, start[, end]]) -> int

        Return the number of non-overlapping occurrences of substring sub in
        string S[start:end].  Optional arguments start and end are
        interpreted as in slice notation.
        """
        return 0
    # 子序列個數
    # >>> a = "Abelisgood"
    # >>> b = a.count('o')
    # >>> b
    # 2
    # >>> b = a.count('o', 0, 8)
    # >>> b
    # 1

功能4:endswith(判斷是否以**結束)

def endswith(self, suffix, start=None, end=None):  # real signature unknown; restored from __doc__
        """
        S.endswith(suffix[, start[, end]]) -> bool

        Return True if S ends with the specified suffix, False otherwise.
        With optional start, test S beginning at that position.
        With optional end, stop comparing S at that position.
        suffix can also be a tuple of strings to try.
        """
        return False
    # 判斷str是否以xxx 結束
    # >>> a
    # 'Abelisgood'
    # >>> b = a.endswith('d')
    # >>> b
    # True
    # >>> b = a.endswith('g', 0, 7)
    # >>> b
    # True

功能5:expandtabs (講tab轉換成空格)

 def expandtabs(self, tabsize=8):  # real signature unknown; restored from __doc__
        """
        S.expandtabs(tabsize=8) -> str

        Return a copy of S where all tab characters are expanded using spaces.
        If tabsize is not given, a tab size of 8 characters is assumed.
        """
        return ""
    # 將tab轉換成空格,默認一個tab轉換成8個空格
    # >>> a = "a\tbel"
    # >>> b= a.expandtabs()
    # >>> b
    # 'a       bel'(8個空格)

功能6:find(尋找子序列位置)

 def find(self, sub, start=None, end=None):  # real signature unknown; restored from __doc__
        """
        S.find(sub[, start[, end]]) -> int

        Return the lowest index in S where substring sub is found,
        such that sub is contained within S[start:end].  Optional
        arguments start and end are interpreted as in slice notation.

        Return -1 on failure.
        """
        return 0
    # 尋找子序列位置,如果沒有找到,返回-1
    # index沒有找到,直接報錯
    # >>> a = "asdfjsdakfwejfi"
    # >>> b = a.find('f', 0, 11)
    # >>> b
    # 3
    # >>> b = a.find('z')
    # >>> b
    # -1

功能7:format(字符串格式化)

def format(self, *args, **kwargs):  # known special case of str.format
        """
        S.format(*args, **kwargs) -> str

        Return a formatted version of S, using substitutions from args and kwargs.
        The substitutions are identified by braces ('{' and '}').
        """
        pass
    # 字符串格式化,動態參數。可以拼接字符串。
    # >>> a = "abel {0} good {1}"
    # >>> b = a.format('is', 'man')
    # >>> b
    # 'abel is good man'
    # >>> 
    def format_map(self, mapping):  # real signature unknown; restored from __doc__
        """
        S.format_map(mapping) -> str

        Return a formatted version of S, using substitutions from mapping.
        The substitutions are identified by braces ('{' and '}').
        """
        return ""

功能8:index(返回索引位置)

def index(self, sub, start=None, end=None):  # real signature unknown; restored from __doc__
        """
        S.index(sub[, start[, end]]) -> int

        Like S.find() but raise ValueError when the substring is not found.
        """
        return 0
    # 返回索引位置;可先參數,start:起始位置,end:結束位置,返回一個整數類型(當字符串沒有找到時,會提示報錯信息)
    # >>> a = "asdfjsdakfwejfi"
    # >>> b = a.index('z')
    # Traceback (most recent call last):
    # File "<pyshell#22>", line 1, in <module>
    #     b = a.index('z')
    # ValueError: substring not found

功能9:isalnum(字符數字組合判斷)

def isalnum(self):  # real signature unknown; restored from __doc__
        """
        S.isalnum() -> bool

        Return True if all characters in S are alphanumeric
        and there is at least one character in S, False otherwise.
        """
        return False
    # 字符串都是字符,數字 或是字符數字組合則爲True否則爲False
    # >>> a
    # 'asdfjsdakfwejfi'
    # >>> b = a.isalnum()
    # >>> b
    # True

    # >>> a = ' '
    # >>> b = a.isalnum()
    # >>> b
    # False

功能10:isalpha(字母判定)

def isalpha(self):  # real signature unknown; restored from __doc__
        """
        S.isalpha() -> bool

        Return True if all characters in S are alphabetic
        and there is at least one character in S, False otherwise.
        """
        return False
    # 字符串中有一個爲字母或所有都是字母都 True 否則爲False
    # >>> a
    # 'abel20'
    # >>> b = a.isalpha()
    # >>> b
    # False

功能11:isdecimal(十進制判定)

 def isdecimal(self):  # real signature unknown; restored from __doc__
        """
        S.isdecimal() -> bool

        Return True if there are only decimal characters in S,
        False otherwise.
        """
        return False
    # 如果只有十進制則返回True,否則返回False
    # >>> a = 'abel20'
    # >>> b = a.isdecimal()
    # >>> b
    # False

功能12:isdecimal(全部爲數字判定)

def isdigit(self):  # real signature unknown; restored from __doc__
        """
        S.isdigit() -> bool

        Return True if all characters in S are digits
        and there is at least one character in S, False otherwise.
        """
        return False
    # 元素全部爲數字時返回 True否則返回 False
    # >>> a
    # 'abel20'
    # >>> b = a.isdigit()
    # >>> b
    # False   

功能13:isidentifier(合法標識符判定)

def isidentifier(self):  # real signature unknown; restored from __doc__
        """
        S.isidentifier() -> bool

        Return True if S is a valid identifier according
        to the language definition.

        Use keyword.iskeyword() to test for reserved identifiers
        such as "def" and "class".
        """
        return False
    # 判斷字符串是否是合法的標識符(標識符必須合法)返回bool類型
    # >>> a = '5'
    # >>> b = a.isidentifier()
    # >>> b
    # False
    # >>> a = 'abel342'
    # >>> b = a.isidentifier()
    # >>> b
    # True

功能14:islower(小寫判定)

    def islower(self):  # real signature unknown; restored from __doc__
        """
        S.islower() -> bool

        Return True if all cased characters in S are lowercase and there is
        at least one cased character in S, False otherwise.
        """
        return False
    # 判斷字符串中字母是否都是小寫,返回bool類型
    # >>> a
    # 'abel342'
    # >>> b = a.islower()
    # >>> b
    # True

    # >>> a = 'Abel83'
    # >>> b = a.islower()
    # >>> b
    # False

功能15:isnumeric(判斷字符串中是否都是數字)

def isnumeric(self):  # real signature unknown; restored from __doc__
        """
        S.isnumeric() -> bool

        Return True if there are only numeric characters in S,
        False otherwise.
        """
        return False
    # 判斷字符串中是否都是數字,返回bool類型
    # >>> a = 'abel349'
    # >>> b = a.isnumeric()
    # >>> b
    # False
    # >>> a = '123'
    # >>> b = a.isnumeric()
    # >>> b
    # True

功能16:isprintable(判斷是不是隻包含可打印字符)

    def isprintable(self):  # real signature unknown; restored from __doc__
        """
        S.isprintable() -> bool

        Return True if all characters in S are considered
        printable in repr() or S is empty, False otherwise.
        """
        return False
    # 判斷是不是隻包含可打印字符,返回Bool值
    # >>> a
    # '天天向上\t2016'
    # >>> b = a.isprintable()
    # >>> b
    # False
    # >>> q = 'this is test'
    # >>> b = a.isprintable()
    # >>> b
    # False

功能17:isspace(判斷是不是隻包含空格字符)

 def isspace(self):  # real signature unknown; restored from __doc__
        """
        S.isspace() -> bool

        Return True if all characters in S are whitespace
        and there is at least one character in S, False otherwise.
        """
        return False
    # 判斷是不是隻包含空格字符,返回bool值

功能18:istitle(判斷是不是隻包含空格字符)

    def istitle(self):  # real signature unknown; restored from __doc__
        """
        S.istitle() -> bool

        Return True if S is a titlecased string and there is at least one
        character in S, i.e. upper- and titlecase characters may only
        follow uncased characters and lowercase characters only cased ones.
        Return False otherwise.
        """
        return False
    # 判斷是不是每個詞的首字母是不是大寫,返回Bool值。一般標題會使用

功能19:isupper(判斷字符串是不是都是大寫)

def isupper(self):  # real signature unknown; restored from __doc__
        """
        S.isupper() -> bool

        Return True if all cased characters in S are uppercase and there is
        at least one cased character in S, False otherwise.
        """
        return False
    # 判斷字符串是不是都是大寫,返回bool值

功能20:join(返回通過指定字符分隔的新字符串)

 def join(self, iterable):  # real signature unknown; restored from __doc__
        """
        S.join(iterable) -> str

        Return a string which is the concatenation of the strings in the
        iterable.  The separator between elements is S.
        """
        return ""
    # 返回通過指定字符分隔的新字符串
    # >>> a = ['a', 'b', 'c', 'f']
    # >>> b = "_".join(a)
    # >>> b
    # 'a_b_c_f'

功能21:ljust(左對齊,右添充)

    def ljust(self, width, fillchar=None):  # real signature unknown; restored from __doc__
        """
        S.ljust(width[, fillchar]) -> str

        Return S left-justified in a Unicode string of length width. Padding is
        done using the specified fill character (default is a space).
        """
        return ""
    # 左對齊,右添充,參數width 寬度(fillchar 添充字符,默認是空格)

功能22:lower(轉換爲小寫)

def lower(self):  # real signature unknown; restored from __doc__
        """
        S.lower() -> str

        Return a copy of the string S converted to lowercase.
        """
        return ""
    # 轉換爲小寫

功能23:lstrip(刪除左邊的空白或自定義的字符)

def lstrip(self, chars=None):  # real signature unknown; restored from __doc__
        """
        S.lstrip([chars]) -> str

        Return a copy of the string S with leading whitespace removed.
        If chars is given and not None, remove characters in chars instead.
        """
        return ""
    # 刪除左邊的空白或自定義的字符
    # >>> a = "   Hi, I'm Abel"
    # >>> b = a.lstrip()
    # >>> b
    # "Hi, I'm Abel"

功能24:maketrans(替換相應的字符)

def maketrans(self, *args, **kwargs):  # real signature unknown
        """
        Return a translation table usable for str.translate().

        If there is only one argument, it must be a dictionary mapping Unicode
        ordinals (integers) or characters to Unicode ordinals, strings or None.
        Character keys will be then converted to ordinals.
        If there are two arguments, they must be strings of equal length, and
        in the resulting dictionary, each character in x will be mapped to the
        character at the same position in y. If there is a third argument, it
        must be a string, whose characters will be mapped to None in the result.
        """
        pass
    # str.translate()函數配合使用,替換相應的字符

功能25:partition(拆分字符串)

def partition(self, sep):  # real signature unknown; restored from __doc__
        """
        S.partition(sep) -> (head, sep, tail)

        Search for the separator sep in S, and return the part before it,
        the separator itself, and the part after it.  If the separator is not
        found, return S and two empty strings.
        """
        pass
    # 用於拆分字符串,返回一個包含三個元素的元組;返回一個包含三個元素的元組。如果指定的字符串sep不存在,則返回自己加兩個空元素。
    # >>> a = "Hi, I'm abel"
    # >>> b = a.partition('a')
    # >>> b
    # ("Hi, I'm ", 'a', 'bel')
    # >>> c = a.partition('z')
    # >>> c
    # ("Hi, I'm abel", '', '')

功能26:replace(替換)

def replace(self, old, new, count=None):  # real signature unknown; restored from __doc__
        """
        S.replace(old, new[, count]) -> str

        Return a copy of S with all occurrences of substring
        old replaced by new.  If the optional argument count is
        given, only the first count occurrences are replaced.
        """
        return ""
    # 使用新的字符串替換老的字符串(可選參數count 指定替換的次數,默認是全部)
    # >>> a = "Abelisgood"
    # >>> b = a.replace('o', 'z')
    # >>> b
    # 'Abelisgzzd'
    # >>> b = a.replace('o', 'z', 1)
    # >>> b
    # 'Abelisgzod'

功能27:rfind(反向查找)

def rfind(self, sub, start=None, end=None):  # real signature unknown; restored from __doc__
        """
        S.rfind(sub[, start[, end]]) -> int

        Return the highest index in S where substring sub is found,
        such that sub is contained within S[start:end].  Optional
        arguments start and end are interpreted as in slice notation.

        Return -1 on failure.
        """
        return 0
    # 反向查找,與find 是相反方向 

功能28:rindex(反向索引)

    def rindex(self, sub, start=None, end=None):  # real signature unknown; restored from __doc__
        """
        S.rindex(sub[, start[, end]]) -> int

        Like S.rfind() but raise ValueError when the substring is not found.
        """
        return 0
    # 返回字符串所在的索引,與index 相反方向

功能29:rjust(右對齊,右填充)

def rjust(self, width, fillchar=None):  # real signature unknown; restored from __doc__
        """
        S.rjust(width[, fillchar]) -> str

        Return S right-justified in a string of length width. Padding is
        done using the specified fill character (default is a space).
        """
        return ""
    # 右對齊,右填充。與ljust 相反

功能30:rpartition(反向拆分)

def rpartition(self, sep):  # real signature unknown; restored from __doc__
        """
        S.rpartition(sep) -> (head, sep, tail)

        Search for the separator sep in S, starting at the end of S, and return
        the part before it, the separator itself, and the part after it.  If the
        separator is not found, return two empty strings and S.
        """
        pass
    # 拆分:與 partition相反,只不過是從字符串的右邊開始拆分。

功能31:rsplit(右邊拆分)

def rsplit(self, sep=None, maxsplit=-1):  # real signature unknown; restored from __doc__
        """
        S.rsplit(sep=None, maxsplit=-1) -> list of strings

        Return a list of the words in S, using sep as the
        delimiter string, starting at the end of the string and
        working to the front.  If maxsplit is given, at most maxsplit
        splits are done. If sep is not specified, any whitespace string
        is a separator.
        """
        return []    # 與split類似,從右邊開始拆分,返回一個列表類型,(可選參數maxsplit 每次+- 1  sep 的值默認是空格)

功能32:split(左邊拆分)

def split(self, sep=None, maxsplit=-1):  # real signature unknown; restored from __doc__
        """
        S.split(sep=None, maxsplit=-1) -> list of strings

        Return a list of the words in S, using sep as the
        delimiter string.  If maxsplit is given, at most maxsplit
        splits are done. If sep is not specified or is None, any
        whitespace string is a separator and empty strings are
        removed from the result.
        """
        return []    # 從左邊開始拆分,返回一個列表類型,(可選參數maxsplit 每次+- 1  sep 的值默認是空格)

功能33:split(生成新列表)

    def splitlines(self, keepends=None):  # real signature unknown; restored from __doc__
        """
        S.splitlines([keepends]) -> list of strings

        Return a list of the lines in S, breaking at line boundaries.
        Line breaks are not included in the resulting list unless keepends
        is given and true.
        """
        return []    # 拆分多行字符串,以每行一個元素生成一個新的列表,如果是單行字符串,則返回原字符串

功能34:startswith(是否以字符串開始)

def startswith(self, prefix, start=None, end=None):  # real signature unknown; restored from __doc__
        """
        S.startswith(prefix[, start[, end]]) -> bool

        Return True if S starts with the specified prefix, False otherwise.
        With optional start, test S beginning at that position.
        With optional end, stop comparing S at that position.
        prefix can also be a tuple of strings to try.
        """
        return False
    # 是否以字符串開始(可選參數,start與end 分別代表起始和結束),返回bool值

功能35:strip(去除左邊空白或自定義字符串)

def strip(self, chars=None):  # real signature unknown; restored from __doc__
        """
        S.strip([chars]) -> str

        Return a copy of the string S with leading and trailing
        whitespace removed.
        If chars is given and not None, remove characters in chars instead.
        """
        return ""
    # 去除左邊空白或自定義字符串

功能36:swapcase(把字符串的大小寫字母互換輸出)

 def swapcase(self):  # real signature unknown; restored from __doc__
        """
        S.swapcase() -> str

        Return a copy of S with uppercase characters converted to lowercase
        and vice versa.
        """
        return ""
    # 把字符串的大小寫字母互換輸出

功能37:title(字符串首字母大寫,其他全部小寫)

 def title(self):  # real signature unknown; restored from __doc__
        """
        S.title() -> str

        Return a titlecased version of S, i.e. words start with title case
        characters, all remaining cased characters have lower case.
        """
        return ""
    # 字符串首字母大寫,其他全部小寫

功能38:translate(str.maketrans()函數配合使用,替換相應的字符)

def translate(self, table):  # real signature unknown; restored from __doc__
        """
        S.translate(table) -> str

        Return a copy of the string S in which each character has been mapped
        through the given translation table. The table must implement
        lookup/indexing via __getitem__, for instance a dictionary or list,
        mapping Unicode ordinals to Unicode ordinals, strings, or None. If
        this operation raises LookupError, the character is left untouched.
        Characters mapped to None are deleted.
        """
        return ""
    # str.maketrans()函數配合使用,替換相應的字符

功能39:upper(全部轉換爲大寫)

def upper(self):  # real signature unknown; restored from __doc__
        """
        S.upper() -> str

        Return a copy of S converted to uppercase.
        """
        return ""
    # 與lower 相反,全部轉換爲大寫

功能40:zfill(回一個添充的字符串)

def zfill(self, width):  # real signature unknown; restored from __doc__
        """
        S.zfill(width) -> str

        Pad a numeric string S with zeros on the left, to fill a field
        of the specified width. The string S is never truncated.
        """
        return ""
    # 回一個添充的字符串,width 添寫長度,如果長寬和字符串相等,則直接返回字符串本身,如果長度大小字符串,則用0添充至指定的長度
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章