Python基礎第二章 二進位 字元編碼 基本數據類型 數字 基本數據類型 字元串 基本數據類型 列表 基本數據類型 元組 可變、不可變數據類型和hash 基本數據類型 字典 基本數據類型 集合 二進位 二進位是計算技術中採用的一種進位。二進位數由0和1兩個數位組成,它的基數為2,進位規則是“逢二進 ...
Python基礎第二章
- 二進位
- 字元編碼
- 基本數據類型-數字
- 基本數據類型-字元串
- 基本數據類型-列表
- 基本數據類型-元組
- 可變、不可變數據類型和hash
- 基本數據類型-字典
- 基本數據類型-集合
二進位
二進位是計算技術中採用的一種進位。二進位數由0和1兩個數位組成,它的基數為2,進位規則是“逢二進一”,由18世紀德國數理哲學大師萊布尼茲發現。當前的電腦系統使用的基本上是二進位系統,數據在電腦中主要是以補碼的形式存儲的。電腦中的二進位則是一個非常微小的開關,用“開”代表1,“關”代表0。
二進位與十進位的轉換
二進位的第n位代表的十進位值都剛好遵循著2的n次方這個規律
裝水桶法
先把他們代表的值一次寫出來,然後再根據10進位的值把數填到相應位置,就好了~~~
十進位轉二進位的方法相同,只要對照二進位為1的那一位對應的十進位值相加就可以了。
128 64 32 16 8 4 2 1
20 0 0 0 1 0 1 0 0
200 1 1 0 0 1 0 0 0
字元編碼
十進位與二進位之間的轉換隻能解決電腦理解數字的問題,那麼為文字的話需要怎麼讓電腦去理解呢?
於是就有了一種曲線救國的方法,既然數字可以轉換成十進位,那麼我們只需要想辦法解決文字轉換成數字的問題,那麼文字不就是可以表示成二進位了嗎?
可是文字應該怎麼轉換成二進位呢? 就是強制轉換
我們自己強行約定了一個表,把文字和數字對應上,這張表就等於是進行了翻譯,我們可以拿一個數字來對比對應表中的文字,反之亦然。
ASCII碼
ASCII(American Standard Code for Information Interchange,美國信息交換標準代碼)是基於拉丁字母的一套電腦編碼系統,主要用於顯示現代英語和其他西歐語言。它是現今最通用的單位元組編碼系統,並等同於國際標準ISO/IEC 646。
由於電腦是美國人發明的,因此,最早只有127個字母被編碼到電腦里,也就是大小寫英字母、數字和一些符號,這個編碼表被稱為ASCII編碼,比如大寫字母 A的編碼是65,小寫字母 z的編碼是122。後128個稱為擴展ASCII碼。
那現在我們就知道了上面的字母符號和數字對應的表是早就存在的。那麼根據現在有的一些十進位,我們就可以轉換成二進位的編碼串。
比如:
一個空格對應的數字是0 翻譯成二進位就是0(註意字元0和整數0是不同的)
一個對勾√對應的數字是251 翻譯成二進位就是11111011
提問:如果我們要列印兩個空格和一個對勾,寫成二進位就應該是[`0011111011],但是問題來了,我們怎麼知道從哪到哪是一個字元呢?聰明的人類就想出了一個解決辦法,既然一共就這255個字元,那最長的也不過11111111八位,不如就把所有的二進位都轉換成8位的,不足的用0來替換。
這樣一來,剛剛的兩個空格一個對勾就寫作[`000000000000000011111011],讀取的時候只要每次讀8個字元就能知道每個字元的二進位值啦。
在這裡,[`每一位0或者1所占的空間單位為bit(比特)],這是電腦中最小的表示單位。每8個bit組成一個位元組,這是電腦最小的存儲單位。
bit 位,電腦中最小的表示單位
8bit = 1bytes 位元組,最小的存儲單位,1bytes縮寫為1B
1KB = 1024KB
1MB = 1024KB
1GB = 1024MB
1TB = 1024GB
1PB = 1024TB
......
那麼,到了現在學完了ASCII碼,作為一名英文程式員來說,基本是圓滿了,但是作為一名中國程式員,是不是覺得還少了點什麼?
GBK和GB2312
很顯然,對於我們來說,能在電腦中顯示中文才是最重要的,可是在剛纔ASCII表裡連一個偏旁部首都沒有。所以我們需要一張關於中文和數字對應的關係表。之前我們看到,1個位元組最多表示256個字元,要處理中文顯然一個位元組是不夠的,所以我們需要採用2個位元組來表示,而且還不能和ASCII編碼衝突,所以中國制訂了GB2312編碼,用來把中文編進去。
那麼此時,又出現了問題:全世界由一百多種語言,當國外某個游戲或者電影引到中國來時,那麼編碼需要怎麼處理?
Unicode
因此,Unicode應運而生,Unicode把所有語言都統一到一套編碼里,這樣就不會再有亂碼問題了。
Unicode標準也在不斷發展,但最常用的是用兩個位元組表示一個字元(如果要用到非常偏僻的字元,就需要4個位元組)。現代操作系統和大多數編程語言都直接支持Unicode。
現在,捋一捋ASCII編碼和Unicode編碼的區別:
ASCII編碼是1個位元組,而Unicode編碼通常是2個位元組。
字母A用ASCII編碼是十進位的65,二進位的01000001;
字元0用ASCII編碼是十進位的48,二進位的00110000;
漢字“中”已經超出了ASCII編碼的範圍,用Unicode編碼是十進位的20013,二進位的01001110 00101101。
你可以猜測,如果把ASCII編碼的A用Unicode編碼,只需要在前面補0就可以,因此,A的Unicode編碼是00000000 01000001。
新的問題又出現了,如果都改成Unicode編碼的話,亂碼問題就會消失,那麼隨之而來的就是:如果你寫的文本基本上都是英文的話,用Unicode編碼要比ASCII編碼需要多一倍的空間,在存儲和傳輸上就十分不划算了。
UTF-8
所以,本著節約的精神,又出現了把Unicode編碼轉化為“可變長編碼”的UTF-8編碼。UTF-8編碼把一個Unicode字元根據不同的數字大小編碼成1-6個位元組,常用的英文字母被編碼成1個位元組,漢字通常是3個位元組,只有很生僻的字元才會被編碼成4-6個位元組。如果你要傳輸的文本包含大量英文字元,用UTF-8編碼就能節省空間:
字元 ASCII Unicode UTF-8
A 01000001 00000000 01000001 01000001
中 x 01001110 00101101 11100100 10111000 10101101
從上圖來看,UTF-8編碼有一個額外的好處,就是ASCII編碼實際上可以看作為UTF-8的一部分,所以,大量只支持ASCII編碼的歷史遺留軟體可以在UTF-8編碼下繼續工作。
搞清楚了ASCII和、Unicode和UTF-8的區關係,可以總結一下電腦系統通用的字元編碼工作方式:
在電腦記憶體中,統一使用Unicode編碼,當需要保存到硬碟或者是需要傳輸的時候,就轉換為UTF-8編碼;
用記事本編輯的時候,從文件讀取的UTF-8字元被轉換成Unicode字元到記憶體里,編輯完成後,保存的時候再把Unicode轉換成UTF-8保存到文件;
- windows預設是GBK
- MacOS\Linux預設為UTF-8
- Python2編碼為ASCII
- Python3編碼為UTF-8
基本數據類型-數字
布爾型
bool型只有兩個值:True和False
之所以將bool值歸類為數字,是因為我們也習慣用1表示True,0表示False
整型
Python中的整數屬於int類型,預設用十進位表示,此外也支持二進位,八進位,十六進位表示方式。
進位轉換
雖然電腦只認識二進位,但是為了迎合我們的習慣,python的數字預設還是十進位。還提供了一些方法來幫助我們做轉換,比如是進位轉換為二進位使用[`bin] 方法,在轉換結果面前還會加上0b表示是一個二進位的數。
>>> num = 132
>>> bin(num)
'0b10000100'
既然十進位可以轉換為二進位,那麼其實使用同樣的原理也可以轉換為其他進位,python也為我們提供了十進位轉換成八進位和十六進位的方法,分別是oct和hex。八進位前面以0o表示 ,十六進位以0表示
>>> num = 129
>>> oct(num)
'0o201'
>>> hex(num)
'0x81'
取餘運算
>>> 16%5
1
divmod
>>> divmod(16,3)
(5, 1) #5為商,1為餘數
浮點型
浮點數是屬於有理數中某特定子集的數的數字表示,在電腦中用以近似表示任意某個實數。具體的說,這個實數由一個整數或定點數(即尾數)乘以某個基數(電腦中通常是2)的整數次冪得到,這表示方法類似於基數為10的科學計數法。
python的浮點數就是數學中的小數(有限小數和無限迴圈小數)
在運算中,整數與浮點數運算的結果也是一個浮點數
為什麼要用浮點數
浮點數也就是小數,之所以稱為浮點數,是因為按照科學記數法表示時,
一個浮點數的小數點位置是可變的,比如,
1.23x109和12.3x108是相等的。
浮點數可以用數學寫法,如1.23,3.14,-9.01,等等。但是對於很大或很小的浮點數,就必須用科學計數法表示,把10用e替代:
1.23*109就是1.23e9,或者12.3e8,0.000012可以寫成1.2e-5,等等。
整數和浮點數在電腦內部存儲的方式是不同的,整數運算永遠是精確的而浮點數運算則可能會有四捨五入的誤差。
關於小數不精準問題
python預設是17位精準度,也就是小數點的後16位,儘管有16位,但這個精確度還是越往後越不准的。
首先,這個問題不是只存在在python中,其他語言也有同樣的問題
其次,小數不精準是因為在轉換成二進位的過程中會出現無限迴圈的情況,在約省的時候就會出現偏差。
比如:11.2的小數部分0.2轉換為2進位則是無限迴圈的00110011001100110011...
單精度在存儲的時候用23bit來存放這個尾數部分(前面9比特存儲指數和符號);同樣0.6也是無限迴圈的;
這裡有一個問題,就是當我們的計算需要更高的精度(超過16位數)的情況下怎麼做呢?
#這裡需要藉助decimal模塊的getcontext()和Decimal()方法
>> a = 3.141592653513651054608317828332
>>> a
3.141592653513651
>>> from decimal import *
>>> getcontext()
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[], traps=[InvalidOperation, DivisionByZero, Overflow])
>>> getcontext().prec = 50 #將小數點後的尾數修改到50
>>> a = Decimal(1)/Decimal(3) #在分數計算中結果正確,如果直接定義超長精度小數不會準確
>>> a
Decimal('0.33333333333333333333333333333333333333333333333333')
>>> a = 3.141592653513651054608317828332
>>> a
3.141592653513651
>>> Decimal(a)
Decimal('3.141592653513650912344701282563619315624237060546875')
複數
複數complex是由實數和虛數組成的。
要瞭解複數,其實關於複數還需要先瞭解虛數。虛數(就是虛假不實的數):平方為複數的數叫做虛數。
複數是指能寫成如下形式的數a+bi,這裡a和b是實數,i是虛數單位(即-1開根)。在複數a+bi中,a稱為複數的實部,b稱為複數的虛部(虛數是指平方為負數的數),i稱為虛數單位。
當虛部等於零時,這個複數就是實數;當虛部不等於零時,這個複數稱為虛數。
註,虛數部分的字母j大小寫都可以。
基本數據類型-字元串
字元串的定義和創建
字元串是一個有序的字元的集合,用於存儲和表示基本的文本信息,''或“”或“”“中間包含的記憶體稱之字元串
創建:
s = 'Hello XiaoYafei!Hello Python!'
字元串的特性與常用操作
特性
1.按照從左往右的順序定義字元集合,下表從0開始順序訪問,有序
str = hello
索引 01234
補充:
1.字元串的單引號和雙引號都無法取消特殊字元的含義,如果想讓引號內所有字元均取消特殊意義,在引導前面添加r,如:
python name = r'pytho\tn'
- 使用ctrl加上滑鼠左鍵可以查看源碼
class str(object):
"""
str(object='') -> str
str(bytes_or_buffer[, encoding[, errors]]) -> str
Create a new string object from the given object. If encoding or
errors is specified, then the object must expose a data buffer
that will be decoded using the given encoding and error handler.
Otherwise, returns the result of object.__str__() (if defined)
or repr(object).
encoding defaults to sys.getdefaultencoding().
errors defaults to 'strict'.
"""
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 ""
def casefold(self): # real signature unknown; restored from __doc__
"""
S.casefold() -> str
Return a version of S suitable for caseless comparisons.
"""
return ""
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 ""
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
def encode(self, encoding='utf-8', errors='strict'): # real signature unknown; restored from __doc__
"""
S.encode(encoding='utf-8', errors='strict') -> bytes
Encode S using the codec registered for encoding. Default encoding
is 'utf-8'. errors may be given to set a different error
handling scheme. Default is 'strict' meaning that encoding errors raise
a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and
'xmlcharrefreplace' as well as any other name registered with
codecs.register_error that can handle UnicodeEncodeErrors.
"""
return b""
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
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 ""
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
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
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 ""
def index(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.index(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.
Raises ValueError when the substring is not found.
"""
return 0
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
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
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
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
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
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
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
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
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
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
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
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 ""
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 ""
def lower(self): # real signature unknown; restored from __doc__
"""
S.lower() -> str
Return a copy of the string S converted to lowercase.
"""
return ""
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 ""
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
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
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 ""
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
def rindex(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
"""
S.rindex(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.
Raises ValueError when the substring is not found.
"""
return 0
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 ""
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
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 []
def rstrip(self, chars=None): # real signature unknown; restored from __doc__
"""
S.rstrip([chars]) -> str
Return a copy of the string S with trailing whitespace removed.
If chars is given and not None, remove characters in chars instead.
"""
return ""
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 []
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 []
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
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 ""
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 ""
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 ""
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 ""
def upper(self): # real signature unknown; restored from __doc__
"""
S.upper() -> str
Return a copy of S converted to uppercase.
"""
return ""
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 ""
常見操作
字元串是不可變的?
>>> a = 'xiaoyafei' #變數的複製,即是將這個變數指向記憶體空間中的地址
>>> id(a) #查看到此時變數a的空間地址
139742341438256
>>> a = 'xiaobaba' #對a進行修改
>>> id(a) #再次查看變數a的空間地址
139742341436848
為什麼說字元串是不可變的呢?我們可以很清晰的看到變數a兩次的空間地址並不一樣,是因為在我們修改時,把a指向了另一個記憶體空間地址,而xiaoyafei這個值將會被垃圾回收
swapcase() 小寫變大寫,大寫變小寫
>>> str = 'Hello World'
>>> str.swapcase()
'hELLO wORLD'
capitalize() 首字母大寫,其餘全變成小寫
>>> str = 'helLo WORLD'
>>> str.capitalize()
'Hello world'
casefold() 把大寫都全部換成小寫
>>> str = 'HELLO WORLD!'
>>> str.casefold()
'hello world!'
center() 返回字元串在中間,兩邊以字元填充
>>> str = 'hello world'
>>> str.center(50,'*')
'*******************hello world********************'
count() 統計字元出現的次數
>>> str = 'abcdfdawadsqasacsasasaa'
>>> str.count('a')
9
#也可以指定從哪開始到哪結束
>>> str.count('a',0,10)
3
endswith() 判斷是否以某字元串結尾,返回True或False
>>> str = 'hello world!python'
>>> str.endswith('python')
True
>>> str.endswith('hello')
False
expandtabs() 擴展tab鍵
>>> a = 'a\tb'
>>> a
'a\tb'
>>> print(a)
a b
>>> a.expandtabs(20)
'a b'
find() 查找,找到則返回索引,找不到則返回-1
>>> name = 'xiaoyafei'
>>> name.find('o')
3
>>> name.find('g')
-1
#也可以規定搜索範圍
>>> name.find('a',4,9)
5
format() 格式化字元串
>>> info = 'my name is {0},i am {1} years old.'
>>> info.format('xiaoyafei',22)
'my name is xiaoyafei,i am 22 years old.'
#也可以使用變數
>>> info = 'my name is {name},my age is {age}.'
>>> info.format(name = 'xiaoyafei',age=22)
'my name is xiaoyafei,my age is 22.'
index() 返回索引值,找不到則報錯
>>> s = 'hello tairan and welcome!'
>>> s.index('t')
6
>>> s.index('g')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
#也可以根據開始和終止位置查找
>>> s
'hello tairan and welcome!'
>>> s.index(
... 'a')
7
isalnum() 判斷是不是阿拉伯字元(數字和字母)
>>> a = '123abc'
>>> a.isalnum()
True
>>> b = '123!abc'
>>> b.isalnum()
False
isalpha() 判斷是否只包含字木,不包含數字
>>> a = 'abc123'
>>> a.isal
a.isalnum( a.isalpha(
>>> a.isalpha()
False
>>> b = 'abcdefg'
>>> b.isalpha()
True
decimal() 判斷是否是整數
>>> a = '111'
>>> a.isdecimal()
True
#第二種方法
>>> a.isdigit()
True
isidentifier() 判斷字元串的值是不是可以使用的變數名
>>> a = '111'
>>> a.isidentifier()
False #數字不能作為變數名
>>> b = 'Menu'
>>> b.isidentifier()
True
islower() 判斷字元串是不是都是小寫
>>> str = 'HelloWorld'
>>> str.islower()
False
>>> str2 = 'helloworld'
>>> str2.islower()
True
isnumeric() 判斷是不是只有數字在裡面
>>> a = '1233'
>>> a.isnumeric()
True
>>> b = 'abc123'
>>> b.isnumeric()
False
isspace() 判斷是不是空格
>>> a = ' '
>>> a.isspace()
True
title() 變成title,即每一個單詞的首字母都是大寫
>>> str = 'hello world python tairan '
>>> str.title()
'Hello World Python Tairan '
upper() 將字元串全部轉換成大寫
python >> namew = 'xiaoyafei' >> namew.upper() 'XIAOYAFEI'
join() 把列表變成字元串後,以指定的字元串來區分列表裡的元素
>>> names = ['lipeng','likun','lipeng']
>>> str = '---'
>>> str.join(names)
'lipeng---likun---lipeng'
ljust() 從左邊開始,總長度不夠的話用指定的字元去代替
>>> name = 'xiaoyafei'
>>> name.ljust(50,'_')
'xiaoyafei_________________________________________'
lower() 把字元串都變成小寫
>>> name = 'XIAOYAFEI'
>>> name.lower()
'xiaoyafei'
strip() 去除兩邊的空格和換行
>>> str = '\n hello world '
>>> str.strip()
'hello world'
# lstrp只去左邊不去右邊
# rstrip只去右邊不去左邊
maketrans() 密碼表
>>> str_in = '1234567890' #這個為原本的
>>> str_out = '!@#$%^&*()' #這個是輸出後的
>>> table = str.maketrans(str_in,str_out) #將兩張表進行對應關係
>>> s = '572428582' #重新輸入的
>>> s.translate(table) #輸出後的密碼表
'%&@$@*%*@'
partition() 把整個字元串以自己想要的進行切割
>>> str
'hello world'
>>> str.partition('o')
('hell', 'o', ' world')
replace() 替換,預設全部更換掉
>>> s = 'hello world,hello tairan'
>>> s.replace('llo','LLO')
'heLLO world,heLLO tairan'
#只更換一次
>>> s
'hello world,hello tairan'
>>> s.replace('llo','LLO',1)
'heLLO world,hello tairan'
rsplit() 從右邊開始將字元串以指定的字元切割
> s
'hello world,hello tairan'
>>> s.rsplit('o')
['hell', ' w', 'rld,hell', ' tairan']
splitlines() 按行來分,格式為列表
>>> str = 'a\nb\nc\nd\n'
>>> str.splitlines()
['a', 'b', 'c', 'd']
startswith() 判斷以指定字元開始
>>> str
'hello,xiaoyafei'
>>> str.startswith('hel')
True
zfill() 從左開始,以指定字元代替,(右邊為原字元串)