string 字串

型別為str

使用type()函式把string的型別輸出,輸出結果為str。

1
2
str1 = "Hello"
print(f"type(str1) = {type(str1)}")
type(str1) = <class 'str'>

語法

使用雙引號""

1
2
str1 = "Hello"
print(f"str1 = {str1}")
str1 = Hello

使用單引號''

Python沒有字元(C++、Java有字元),單引號包住的是字串。

1
print(type('A'))
<class 'str'>

以下範例使用單引號''

1
2
str2 = 'Hello'
print(f"str2 = {str2}")
str2 = Hello

雙引號包住單引號 單引號包住雙引號

雙引號包住單引號

1
2
str3 = "Hi 'Amy'"
print(f"str3 = {str3}")
str3 = Hi 'Amy'

單引號包住雙引號

1
2
str4 = 'Hi "Amy"'
print(f"str4 = {str4}")
str4 = Hi "Amy"

三個雙引號與三個雙引號

使用三個雙引號""" 內容 """ 被三個雙引號包住的,裡面的格式都可以保留。

1
2
3
4
5
6
7
8
str5 = """
def max(a, b):
    if a > b:
        return a
    else:
        return b
"""
print(f"str5 = {str5}")
str5 = 
def max(a, b):
    if a > b:
        return a
    else:
        return b

使用三個單引號''' 內容 ''' 被三個雙引號包住的,裡面的格式都可以保留。

1
2
3
4
5
6
7
8
str5 = '''
def max(a, b):
    if a > b:
        return a
    else:
        return b
'''
print(f"str5 = {str5}")
str5 = 
def max(a, b):
    if a > b:
        return a
    else:
        return b

+號字串相連

雙引號與單引號都是字串,字串的型別,使用+號,代表把二個字串連起來。

1
2
str1 = "Hello" + 'A'
print(f"str1 = {str1}")
str1 = HelloA

* 字串重覆次數

語法

字串 * 10

相同的字串重覆10次並連在一起。
以下程式碼,Hi重覆5次並連在一起。
-重覆5次並連在一起。

1
2
print("Hi" * 5)
print("-" * 5)
HiHiHiHiHi
-----

r與跳脫字元

\n\t都是跳脫字元。
在雙引號或單引號最前面加上r,字串中有\n就不會斷行,反而直接輸出\n

1
2
str1 = r"Hello\n World \t Hi"
print(f"str1 = {str1}")
str1 = Hello\n World \t Hi

字串Memory Layout

id的文章中有討論到字串駐留。

字串駐留的條件:

  • 字串是由大小寫英文字母、0-9、底線_
  • 字串長度為0或1

產生字串「物件」會存到Heap中,Memory中stack是儲存變數,Memory中Heap是儲存物件。

物件本身的記憶體位址是0x0100。
物件中有記錄指向H的記憶體位址0x0011。
物件中有記錄指向i的記憶體位址0x0022。

img

以下程式碼顯示字串物件記憶體位址為4488577600。
H的記憶體位址為4498451176。 i的記憶體位址為4498452760。

1
2
3
4
str1 = "Hi"
print(f"str1 = {str1}, id = {id(str1)}")
print(f"H address = {id(str1[0])}")
print(f"i address = {id(str1[1])}")
str1 = Hi, id = 4488577600
H address = 4498451176
i address = 4498452760

以下程式碼顯示,不同變數的內容都為Hi,實際上都指向相同字串物件記憶體位址4488577600。

1
2
3
4
5
6
7
8
str1 = "Hi"
print(f"str1 = {str1}, id = {id(str1)}")
str2 = 'Hi'
print(f"str2 = {str2}, id = {id(str2)}")
str3 = """Hi"""
print(f"str3 = {str3}, id = {id(str3)}")
str4 = '''Hi'''
print(f"str4 = {str4}, id = {id(str4)}")
str1 = Hi, id = 4488577600
str2 = Hi, id = 4488577600
str3 = Hi, id = 4488577600
str4 = Hi, id = 4488577600

編碼

ord()

字串中每個單字是由Unicode組成,Python提供ord()可以查詢Unicode碼。
參數可以是雙單號,也可以是單引號,二者皆為字串。

1
2
print(ord('A'))
print(ord("A"))
65
65

chr()

透過 ASCII 或 Unicode 得到對應的字元。

1
2
3
print(chr(65))
print(chr(97))
print(chr(48))
A
a
0

字串比較

字串可以比較,比較的運算子有 > < >= <= == !=

會根據ord()函式一個一個字母比對大小。

1
2
3
str1 = "abc"
str2 = "ab"
print(str1 < str2)
False

[索引]取出字串中的字元

使用[索引]取出字串中的單字,語法:

字串變數[索引]
1
2
3
4
str1 = "Hello"
print(str1[0])
print(str1[1])
print(str1[2])
H
e
l

Python沒有字元,透過索引取出來的單字,型別仍是字串。

1
2
str1 = "Hello"
print(f"str1[0] = {str1[0]} , type = {type(str1[0])}")
str1[0] = H , type = <class 'str'>

無法透過索引修改字串的單字

以下程式碼會編譯錯誤。

1
2
str1 = "Hello"
st1[0] = "A"

字串遍歷

for

1
2
3
str1 = "Hello"
for elem in str1:
    print(elem)
H
e
l
l
o

while

1
2
3
4
5
str1 = "Hello"
index = 0
while index < len(str1):
    print(str1[index])
    index += 1

負數索引

-1為字串最後一位,-2為倒數第2位。

字串 H e l l o
正數索引 0 1 2 3 4
負數索引 -5 -4 -3 -2 -1
1
2
3
4
str1 = "Hello World"
print(str1[-1])
print(str1[-2])
print(str1[-3])
d
l
r

字串相關函式

str.capitalize() 第一個字母大寫

1
2
str1 = "hello"
print(str1.capitalize())
Hello

str.upper() 全部大寫

1
2
str1 = "hello"
print(str1.upper())

str.lower() 全部小寫

1
2
str1 = "hello"
print(str1.lower())
hello

len()字串長度

1
2
str1 = "hello"
print(len(str1))
5

str.count()字串出現次數

單字出現次數

1
2
str1 = "hello"
print(str1.count("l"))
2

字串出現次數

1
2
str1 = "Hello world world"
print(str1.count("world"))
2

str1.index() 尋找字串所在索引位置

尋找單字

1
2
str1 = "hello"
print(str1.index("l"))
2

尋找字串第一個字母所在位置。

1
2
str1 = "Hello world world"
print(str1.index("world"))
6

str1.replace() 取代字串

語法

str.replace("舊文字","新文字",取代幾次)

如果第3個參數不寫,預取是取代所有的l。

1
2
str1 = "hello"
print(str1.replace("l","A"))
heAAo

如果第3個參數有寫,以下程式碼是取代1個l。

1
2
str1 = "hello"
print(str1.replace("l","A",1))
heAlo

取代字串。

1
2
str1 = "Hello world world"
print(str1.replace("world", "Mary"))
Hello Mary Mary

str1.strip()去除前後字元

注意!只會去除前後字元,以下程式碼去掉字串前的空白與後面的空白。

1
2
str1 = "   Hello world    "
print(str1.strip(" "))
Hello world

以下程式碼去除字串前後的abc字串。

1
2
str1 = "abcHello abc worldabc"
print(str1.strip("abc"))
Hello abc world

str.split()

根據空格或逗號切割字串,傳回值為list類型

1
2
3
str1 = "Hello World"
result = str1.split(" ")
print(f"result = {result}, type = {type(result)}")
result = ['Hello', 'World'], type = <class 'list'>

str.isalpha() 是否為大小寫字母

以下程式碼判斷若為大小寫字母,把它變為大寫。

1
2
3
4
5
6
str1 = "hello 123"
alpha = ""
for elem in str1:
    if elem.isalpha():
        alpha += elem.upper() + ", "
print(alpha)
H, E, L, L, O, 

results matching ""

    No results matching ""