1. 程式人生 > >python中字串的各種處理(未完待續)

python中字串的各種處理(未完待續)

字串的定義:
由一對單引號或雙引號引起來的內容即是字串。

型別:
字串是不可變型別(無論對字串進行那種操作,字串本身是不會改變的,返回的是被操作後新的字串物件)。

操作字串的方法:

1,字串[]
語法:
字串[要獲取的子字串的下標]
作用:
獲取字串指定下標的子字串

示例A:

string = "first set a small target, i first earn it a billion"
index = string[3]
print(index)
console:s

2,len()方法

語法:
len(要操作的字串)
作用:
返回字串的長度。

例子A:

string = "first set a small target, i first earn it a billion"
length = len(string)
print(length)
 console51

3,coun()方法

語法:
字串.count(子字串)
作用:
返回子字串在字串中出現的次數,如果沒有出現,則返回數字0.

示例A:

string = "first set a small target, i first earn it a billion"
count = string.count("s")
print(count
)
console4

示例B:

string = "first set a small target, i first earn it a billion"
count = string.count("what?")
print(count)
console0

4,index()方法

語法:
字串.index(要查詢的子串,起始下標,結尾下表)
當然,後兩個引數可以不宣告。
作用:查詢指定的子字串,如果找到則返回子串的下標(索引),
如果沒找到則報錯。

示例A:

# 查詢“ar”第一次出現的下標。
string = "first set a small target, i first earn it a billion"
index = string.index("ar", 19) print(index)
console19

示例B:

# 查詢“ar”除去第一次後,出現的下標。
string = "first set a small target, i first earn it a billion"
index = string.index("ar", 20)
print(index)
console35
# 看一下沒有找到的情況
string = "first set a small target, i first earn it a billion"
index = string.index("Bigbang")
print(index)
console:
Traceback (most recent call last):
  File "D:/Python/PycharmProjects/happy time/new_text.py", line 3, in <module>
    index = string.index("Bigbang")
ValueError: substring not found

5,rindex()方法

語法:
用法和index()方法一樣,不過從右邊開始查詢。

示例A:

# 查詢“ar”最後一次出現的下標。
string = "first set a small target, i first earn it a billion"
index = string.rindex("ar")
print(index)
console35

6,find()方法

(跟index方法一樣)
字串.find(要查詢的子串,起始下標,結尾下表)
當然,後兩個引數可以不宣告。
作用:
查詢指定的子字串,但與index方法不同的是如果找到則返回子串的下標(索引),如果沒找到則返回-1。(實際應用中find方法更常用)
示例A:

string = "first set a small target, i first earn it a billion"
count = string.find("whf")
print(count)
console:-1

7,rfind()方法

語法:
用法和rfind()方法一樣,不過從右邊開始查詢。

示例A:

string = "first set a small target, i first earn it a billion"
count = string.rfind("a")
print(count
console42

8,isdigit()方法

語法:
字串.isdigit()
作用:
判斷整個字串是否全部為整數(不是數字),如果是則返回True,否則返回False。

示例A:

string = "first set a small target, i first earn it a billion"
count = string.isdigit()
print(count)
console :False

示例B:

# 字串是整數的情況
string2 = "456789"
red = string2.isdigit()
print(red)
console: True

示例C:

# 字串是小數的情況
string2 = "456789.67"
red = string2.isdigit()
print(red)
console:False

9,isalpha()方法

語法:
字串.isalpha()
作用:
判斷字串是否為純數字組成,如果是則返回True,否則返回False。

示例A:

string = "wtf"
count = string.isalpha()
print(count)
console:True

示例B:

string = "first set a small target, i first earn it a billion"
count = string.isalpha()
print(count)
console: False

10,isspace()方法

語法:
字串.isspace()
作用:
判斷字串是否以純空格組成,如果是,則返回True,否則返回False。
示例A:

string = "  "
count = string.isspace()
print(count)
console:True

示例2:

string = "first set a small target, i first earn it a billion"
count = string.isalpha()
print(count)
console:False

11,replace()函式

語法:
字串.replace(老子字串,新子字串,替換的次數)
作用:
通過replace方法將字串中指定的老子字串替換為新的子字串,如果不指定替換的次數,則預設全部替換。

示例A:

string = "first set a small target, i first earn it a billion"
count = string.replace("a", "A")
print(count)
console:

first set A smAll tArget, i first eArn it A billion

示例B:

#  指定替換的次數
string = "first set a small target, i first earn it a billion"
count = string.replace("a", "A", 2)
print(count)
console:

first set A smAll target, i first earn it a billion

12,startswith()方法

語法:
字串.startswith(“子字串”)
作用:
判斷字串是否由指定的子字串開頭,是則返回True,否則返回False

示例A:

string = "first set a small target, i first earn it a billion"
count = string.startswith("first")
print(count)
console:True

示例B:

string = "first set a small target, i first earn it a billion"
count = string.startswith("friss")
print(count)
console:False

13,endswith()方法

語法:
字串.endswith(“子字串”)
作用:
判斷字串是否由指定的子字串結尾,是則返回True,否則返回False

示例A:

string = "first set a small target, i first earn it a billion"
count = string.endswith("billion")
print(count)
console:True

示例B:

string = "first set a small target, i first earn it a billion"
count = string.endswith("billoon")
print(count)
console:False

14,strip()函式

語法:
字串.strip()
作用:
去掉字串兩邊的空格。

示例A:

string = "  first set a small target, i first earn it a billion  "
count = string.strip()
print(count)
print(string)
console:
first set a small target, i first earn it a billion
  first set a small target, i first earn it a billion  

另外:
lstrip()和rstrip()函式,他們的語法同strip(),只不過一個是去掉左邊的空格,一個是去掉右邊的空格,這裡不再贅述。

15,split()函式

語法:
字串.split(分割符,分割的次數)
作用:
將字串按分割符分割,(丟失分割符),如果 分割的次數有指定值,則僅分隔 分割次數 + 1 個子字串,並返回一個列表。

示例:

string = "  first set a small target, i first earn it a billion  "
count = string.split(" ")
print(count)
console:
['', '', 'first', 'set', 'a', 'small', 'target,', 'i', 'first', 'earn', 'it', 'a', 'billion', '', '']

示例B:

#  指定分割次數
string = "  first set a small target, i first earn it a billion  "
count = string.split(" ", 5)
print(count)
['', '', 'first', 'set', 'a', 'small target, i first earn it a billion  ']

16,partition()函式

語法:
字串.partition(子字串)
作用:
通過partition()函式,根據子字串,把字串分割為三部分,子字串前,子字串,子字串後,返回一個元祖,

示例A:

string = "  first set a small target, i first earn it a billion  "
count = string.partition("target")
print(type(count))
print(count)
console:
<class 'tuple'>
('  first set a small ', 'target', ', i first earn it a billion  ')

未完待續。。。。。。