Python數據類型方法簡介一————字符串的用法詳解
符串是Python中的重要的數據類型之一,並且字符串是不可修改的。
字符串就是引號(單、雙和三引號)之間的字符集合。(字符串必須在引號之內,引號必須成對)
註:單、雙和三引號在使用上並無太大的區別;
引號之間可以采取交叉使用的方式避免過多轉義;
單、雙引號實現換行必須使用續行符,而三引號可直接續行不必使用續行符號。
a. count,統計字符或子字符串在字符串中出現的次數
格式:S.count(sub[, start[, end]]) -> int
sub 是字符串中要查詢的子字符串 start是開始統計的索引位置 end是結束的索引位置 返回一個數字
>>> s="hello python"
>>> s.count(‘l‘)
>>> 2
>>> s
‘hello python‘
>>> s.count(‘o‘)
2
>>> s.count(‘o‘,4,6)
1
b. capitalize 首字母大寫
格式:S.capitalize() -> string
>>> s
‘hello python‘
>>> s.capitalize()
‘Hello python‘
c.isalnum,isalpha,isdigit,islower,isspace,istitle,isupper
以isdigit判斷字符串是否只含有數字
格式:S.isdigit() -> boo
>>> s
‘hello python‘
>>> s.isdigit()
False
>>> s=‘hello python 1‘
>>> s.isdigit()
False
>>> s=‘123‘
>>> s.isdigit()
True
islower判斷字符串中的字母是否都是小寫字母
格式:S.islower() -> bool
>>> s
‘hello python‘
>>> s.islower()
True
>>> s="Hello python"
>>> s.islower()
False
d. lower/upoer/title把字符串中的字母都變成小寫/大寫/首字母大寫
格式:S.lower() -> string
S.upper() -> string
S.title() -> string
>>> s
‘Hello python‘
>>> s.lower()
‘hello python‘
>>> s.upper()
‘HELLO PYTHON‘
>>> s.title()
‘Hello Python‘
e. startswith/endswith是否以什麽開頭/結尾,返回布爾值
格式:S.startswith(prefix[, start[, end]]) -> bool
S.endswith(suffix[, start[, end]]) -> bool
prefix/suffix開頭/結尾 start開始的索引位置 end開始的索引位置 返回布爾值
>>> s
‘hello python‘
>>> s.startswith(‘he‘)
True
>>> s.startswith(‘e‘)
False
>>> s.startswith(‘he‘,4,8)
False
f. replace字符串替換
格式:S.replace(old, new[, count]) -> string
old被替換的字符串 new要替換的字符串 count替換的次數 返回一個字符串
>>> s
‘hello python‘
>>> s.replace(‘e‘,‘E‘)
‘hEllo python‘
>>> s.replace(‘l‘,‘L‘,)
‘heLLo python‘
>>> s.replace(‘l‘,‘L‘,1)
‘heLlo python‘
g. split/rsplit(從右開始)字符串切割
格式:S.split([sep [,maxsplit]]) -> list of strings
S.rsplit([sep [,maxsplit]]) -> list of strings
sep 指定切割字符 maxsplit 最多切割的次數 返回一個以字符串為元素的列表
>>> s
‘hello python‘
>>> s.split(" ")
[‘hello‘, ‘python‘]
>>> s.split("l",1)
[‘he‘, ‘lo python‘]
>>> s.rsplit("l",1)
[‘hel‘, ‘o python‘]
h. jion 將可叠代的數據類型轉換成一個字符串
格式:S.join(iterable) -> string
>>> s
‘hello python‘
>>> S=s.split(" ")
>>> S
[‘hello‘, ‘python‘]
>>> " ".join(S)
‘hello python‘
i. strip去除字符串指定的首尾字符(單個字符為單位匹配)
格式:S.strip([chars]) -> string or unicode
>>> s
‘hello python‘
>>> s.strip("o")
‘hello python‘
>>> s.strip("n")
‘hello pytho‘
>>> s=‘hello olleh‘
>>> s.strip("he")
‘llo oll‘
>>> s.strip("he")
‘llo olleh ‘
strip這個用法是以字符為單位進行匹配,以最後一個例子,要去除he,當前面匹配到l,不在he中結束,後面匹配到空格,空格不在he中,結束。
j. find查找指定字符的位置,沒有則返回1。找到第一個匹配的字符就返回索引值
格式:S.find(sub [,start [,end]]) -> int
sub 指定的字符 start開始的索引位置,end結束的索引位置。 返回數字
>>> s=‘hello python‘
>>> s.find(‘o‘)
4
>>> s.find(‘o‘,5,11)
10
>>> s.find(‘a‘)
-1
k. index查找字符所在索引位置,沒有報一個異常
格式:S.index(sub [,start [,end]]) -> int
>>> s
‘hello python‘
>>> s.index(‘h‘)
0
>>> s.index(‘h‘,5)
9
>>> s.index(‘a‘)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
l. format字符串格式化
格式:S.format(*args, **kwargs) -> string
args 參數,指定是位置傳參, kwargs 關鍵字傳參
>>> print "my name is {},my age is {}".format(‘xiaoming‘,10)
my name is xiaoming,my age is 10
在Python2.6中必須指定參數的位置
>>> print "my name is {0},my age is {1}".format(‘xiaoming‘,10)
my name is xiaoming,my age is 10
還有一種方式:
>>> print "my name is %s,my age is %d"%(‘xiaoming‘,10)
my name is xiaoming,my age is 10
%s指定數據為字符串,%d指定是數字
這兩種格式化字符串在Python中常用到
字符串連接:
字符串連接方法有三種,字符串的加法與join方法效果一致,但是原理不同,因為python中字符串是不可變的類型,使用 + 連接兩個字符串時會生成一個新的字符串,生成新的字符串就需要重新申請內存,當連續相加的字符串很多時(a+b+c+d+e+f+...) ,效率低下就是必然的了。jion方法使用略復雜,但對多個字符進行連接時效率高,只會有一次內存的申請。這種方法中jion必須是一個可叠代的類型,當然也可以使用字符串格式化的方法進行字符串連接。
字符串加法
>>> "hello"+"python"
‘hellopython‘
字符串的join方法
>>> " ".join(["hello","python"])
‘hello python
字符串格式化
>>> "%s%s%s"%(‘hello‘,‘ ‘,‘python‘)
‘hello python‘
字符串乘法
字符串乘法經常用來打印分隔符。
>>> print "="*10
==========
>>> print "python "*10
python python python python python python python python python python
Python數據類型方法簡介一————字符串的用法詳解