成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Python 字符串深度總結(jié)

開發(fā) 前端
字符串是包含一系列字符的對(duì)象。字符是長(zhǎng)度為 1 的字符串。在 Python 中,單個(gè)字符也是字符串。但是比較有意思的是,Python 編程語言中是沒有字符數(shù)據(jù)類型的,不過在 C、Kotlin 和 Java 等其他編程語言中是存在字符數(shù)據(jù)類型的。

今天我們來學(xué)習(xí)字符串?dāng)?shù)據(jù)類型相關(guān)知識(shí),將討論如何聲明字符串?dāng)?shù)據(jù)類型,字符串?dāng)?shù)據(jù)類型與 ASCII 表的關(guān)系,字符串?dāng)?shù)據(jù)類型的屬性,以及一些重要的字符串方法和操作,超級(jí)干貨,不容錯(cuò)過!

什么是 Python 字符串

字符串是包含一系列字符的對(duì)象。字符是長(zhǎng)度為 1 的字符串。在 Python 中,單個(gè)字符也是字符串。但是比較有意思的是,Python 編程語言中是沒有字符數(shù)據(jù)類型的,不過在 C、Kotlin 和 Java 等其他編程語言中是存在字符數(shù)據(jù)類型的。

我們可以使用單引號(hào)、雙引號(hào)、三引號(hào)或 str() 函數(shù)來聲明 Python 字符串。下面的代碼片段展示了如何在 Python 中聲明一個(gè)字符串:

# A single quote string
single_quote = 'a'  # This is an example of a character in other programming languages. It is a string in Python

# Another single quote string
another_single_quote = 'Programming teaches you patience.'

# A double quote string
double_quote = "aa"

# Another double-quote string
another_double_quote = "It is impossible until it is done!"

# A triple quote string
triple_quote = '''aaa'''

# Also a triple quote string
another_triple_quote = """Welcome to the Python programming language. Ready, 1, 2, 3, Go!"""

# Using the str() function
string_function = str(123.45)  # str() converts float data type to string data type

# Another str() function
another_string_function = str(True)  # str() converts a boolean data type to string data type

# An empty string
empty_string = ''

# Also an empty string
second_empty_string = ""

# We are not done yet
third_empty_string = """"""  # This is also an empty string: ''''''

在 Python 中獲取字符串的另一種方法是使用 input() 函數(shù)。input() 函數(shù)允許我們使用鍵盤將輸入的值插入到程序中。插入的值被讀取為字符串,但我們可以將它們轉(zhuǎn)換為其他數(shù)據(jù)類型:

# Inputs into a Python program
input_float = input()  # Type in: 3.142
input_boolean = input() # Type in: True

# Convert inputs into other data types
convert_float = float(input_float)  # converts the string data type to a float
convert_boolean = bool(input_boolean) # converts the string data type to a bool

我們使用 type() 函數(shù)來確定 Python 中對(duì)象的數(shù)據(jù)類型,它返回對(duì)象的類。當(dāng)對(duì)象是字符串時(shí),它返回 str 類。同樣,當(dāng)對(duì)象是字典、整數(shù)、浮點(diǎn)數(shù)、元組或布爾值時(shí),它分別返回 dict、int、float、tuple、bool 類。現(xiàn)在讓我們使用 type() 函數(shù)來確定前面代碼片段中聲明的變量的數(shù)據(jù)類型:

# Data types/ classes with type()

print(type(single_quote))
print(type(another_triple_quote))
print(type(empty_string))

print(type(input_float))
print(type(input_boolean))

print(type(convert_float))
print(type(convert_boolean))

ASCII 表與 Python 字符串字符

美國(guó)信息交換標(biāo)準(zhǔn)代碼 (ASCII) 旨在幫助我們將字符或文本映射到數(shù)字,因?yàn)閿?shù)字集比文本更容易存儲(chǔ)在計(jì)算機(jī)內(nèi)存中。ASCII 主要用英語對(duì) 128 個(gè)字符進(jìn)行編碼,用于處理計(jì)算機(jī)和編程中的信息。ASCII 編碼的英文字符包括小寫字母(a-z)、大寫字母(A-Z)、數(shù)字(0-9)以及標(biāo)點(diǎn)符號(hào)等符號(hào)

ord() 函數(shù)將長(zhǎng)度為 1(一個(gè)字符)的 Python 字符串轉(zhuǎn)換為其在 ASCII 表上的十進(jìn)制表示,而 chr() 函數(shù)將十進(jìn)制表示轉(zhuǎn)換回字符串。例如:

import string

# Convert uppercase characters to their ASCII decimal numbers
ascii_upper_case = string.ascii_uppercase  # Output: ABCDEFGHIJKLMNOPQRSTUVWXYZ

for one_letter in ascii_upper_case[:5]:  # Loop through ABCDE
    print(ord(one_letter))

Output:

65
66
67
68
69
# Convert digit characters to their ASCII decimal numbers
ascii_digits = string.digits  # Output: 0123456789

for one_digit in ascii_digits[:5]:  # Loop through 01234
    print(ord(one_digit))

Output:

48
49
50
51
52

在上面的代碼片段中,我們遍歷字符串 ABCDE 和 01234,并將每個(gè)字符轉(zhuǎn)換為它們?cè)?ASCII 表中的十進(jìn)制表示。我們還可以使用 chr() 函數(shù)執(zhí)行反向操作,從而將 ASCII 表上的十進(jìn)制數(shù)字轉(zhuǎn)換為它們的 Python 字符串字符。例如:

decimal_rep_ascii = [37, 44, 63, 82, 100]

for one_decimal in decimal_rep_ascii:
    print(chr(one_decimal))

Output:

%
,
?
R
d

在 ASCII 表中,上述程序輸出中的字符串字符映射到它們各自的十進(jìn)制數(shù)

字符串屬性

零索引: 字符串中的第一個(gè)元素的索引為零,而最后一個(gè)元素的索引為 len(string) - 1。例如:

immutable_string = "Accountability"

print(len(immutable_string))
print(immutable_string.index('A'))
print(immutable_string.index('y'))

Output:

14
0
13

不變性: 這意味著我們不能更新字符串中的字符。例如我們不能從字符串中刪除一個(gè)元素或嘗試在其任何索引位置分配一個(gè)新元素。如果我們嘗試更新字符串,它會(huì)拋出 TypeError:

immutable_string = "Accountability"

# Assign a new element at index 0
immutable_string[0] = 'B'

Output:

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

~\AppData\Local\Temp/ipykernel_11336/2351953155.py in 
      2 
      3 # Assign a new element at index 0
----> 4 immutable_string[0] = 'B'

TypeError: 'str' object does not support item assignment

但是我們可以將字符串重新分配給 immutable_string 變量,不過我們應(yīng)該注意它們不是同一個(gè)字符串,因?yàn)樗鼈儾恢赶騼?nèi)存中的同一個(gè)對(duì)象。Python 不會(huì)更新舊的字符串對(duì)象;它創(chuàng)建了一個(gè)新的,正如我們通過 ids 看到的那樣:

immutable_string = "Accountability"
print(id(immutable_string))

immutable_string = "Bccountability"
print(id(immutable_string)

test_immutable = immutable_string
print(id(test_immutable))

Output:

2693751670576
2693751671024
2693751671024

上述兩個(gè) id 在同一臺(tái)計(jì)算機(jī)上也不相同,這意味著兩個(gè) immutable_string 變量都指向內(nèi)存中的不同地址。我們將最后一個(gè) immutable_string 變量分配給 test_immutable 變量。可以看到 test_immutable 變量和最后一個(gè) immutable_string 變量指向同一個(gè)地址

連接: 將兩個(gè)或多個(gè)字符串連接在一起以獲得帶有 + 符號(hào)的新字符串。例如:

first_string = "Zhou"
second_string = "luobo"
third_string = "Learn Python"

fourth_string = first_string + second_string
print(fourth_string)

fifth_string = fourth_string + " " + third_string
print(fifth_string)

Output:

Zhouluobo
Zhouluobo Learn Python

重復(fù): 字符串可以用 * 符號(hào)重復(fù)。例如:

print("Ha" * 3)

Output:

HaHaHa

索引和切片: 我們已經(jīng)確定字符串是從零開始索引的,我們可以使用其索引值訪問字符串中的任何元素。我們還可以通過在兩個(gè)索引值之間進(jìn)行切片來獲取字符串的子集。例如:

main_string = "I learned English and Python with ZHouluobo. You can do it too!"

# Index 0
print(main_string[0])

# Index 1
print(main_string[1])

# Check if Index 1 is whitespace
print(main_string[1].isspace())

# Slicing 1
print(main_string[0:11])

# Slicing 2:
print(main_string[-18:])

# Slicing and concatenation
print(main_string[0:11] + ". " + main_string[-18:])

Output:

I

True
I learned English
You can do it too!
I learned English. You can do it too!

字符串方法

str.split(sep=None, maxsplit=-1): 字符串拆分方法包含兩個(gè)屬性:sep 和 maxsplit。當(dāng)使用其默認(rèn)值調(diào)用此方法時(shí),它會(huì)在任何有空格的地方拆分字符串。此方法返回字符串列表:

string = "Apple, Banana, Orange, Blueberry"
print(string.split())

Output:

['Apple,', 'Banana,', 'Orange,', 'Blueberry']

我們可以看到字符串沒有很好地拆分,因?yàn)椴鸱值淖址?nbsp;,。我們可以使用 sep=',' 在有 , 的地方進(jìn)行拆分:

print(string.split(sep=','))

Output:

['Apple', ' Banana', ' Orange', ' Blueberry']

這比之前的拆分要好,但是我們可以在一些拆分字符串之前看到空格。可以使用 (sep=', ') 刪除它:

# Notice the whitespace after the comma
print(string.split(sep=', '))

Output:

['Apple', 'Banana', 'Orange', 'Blueberry']

現(xiàn)在字符串被很好地分割了。有時(shí)我們不想分割最大次數(shù),我們可以使用 maxsplit 屬性來指定我們打算拆分的次數(shù):

print(string.split(sep=', ', maxsplit=1))

print(string.split(sep=', ', maxsplit=2))

Output:

['Apple', 'Banana, Orange, Blueberry']
['Apple', 'Banana', 'Orange, Blueberry']

str.splitlines(keepends=False): 有時(shí)我們想處理一個(gè)在邊界處具有不同換行符('\n'、\n\n'、'\r'、'\r\n')的語料庫(kù)。我們要拆分成句子,而不是單個(gè)單詞。可以使用 splitline 方法來執(zhí)行此操作。當(dāng) keepends=True 時(shí),文本中包含換行符;否則它們被排除在外

import nltk  # You may have to `pip install nltk` to use this library.

macbeth = nltk.corpus.gutenberg.raw('shakespeare-macbeth.txt')
print(macbeth.splitlines(keepends=True)[:5])

Output:

['[The Tragedie of Macbeth by William Shakespeare 1603]\n', '\n', '\n', 'Actus Primus. Scoena Prima.\n', '\n']

str.strip([chars]): 我們使用 strip 方法從字符串的兩側(cè)刪除尾隨空格或字符。例如:

string = "    Apple Apple Apple no apple in the box apple apple             "

stripped_string = string.strip()
print(stripped_string)

left_stripped_string = (
    stripped_string
    .lstrip('Apple')
    .lstrip()
    .lstrip('Apple')
    .lstrip()
    .lstrip('Apple')
    .lstrip()
)
print(left_stripped_string)

capitalized_string = left_stripped_string.capitalize()
print(capitalized_string)

right_stripped_string = (
    capitalized_string
    .rstrip('apple')
    .rstrip()
    .rstrip('apple')
    .rstrip()
)
print(right_stripped_string)

Output:

Apple Apple Apple no apple in the box apple apple
no apple in the box apple apple
No apple in the box apple apple
No apple in the box

在上面的代碼片段中,我們使用了 lstrip 和 rstrip 方法,它們分別從字符串的左側(cè)和右側(cè)刪除尾隨空格或字符。我們還使用了 capitalize 方法,它將字符串轉(zhuǎn)換為句子大小寫str.zfill(width): zfill 方法用 0 前綴填充字符串以獲得指定的寬度。例如:

example = "0.8"  # len(example) is 3
example_zfill = example.zfill(5) # len(example_zfill) is 5
print(example_zfill)

Output:

000.8

str.isalpha(): 如果字符串中的所有字符都是字母,該方法返回True;否則返回 False:

# Alphabet string
alphabet_one = "Learning"
print(alphabet_one.isalpha())

# Contains whitspace
alphabet_two = "Learning Python"
print(alphabet_two.isalpha())

# Contains comma symbols
alphabet_three = "Learning,"
print(alphabet_three.isalpha())

Output:

True
False
False

如果字符串字符是字母數(shù)字,str.isalnum() 返回 True;如果字符串字符是十進(jìn)制,str.isdecimal() 返回 True;如果字符串字符是數(shù)字,str.isdigit() 返回 True;如果字符串字符是數(shù)字,則 str.isnumeric() 返回 True

如果字符串中的所有字符都是小寫,str.islower() 返回 True;如果字符串中的所有字符都是大寫,str.isupper() 返回 True;如果每個(gè)單詞的首字母大寫,str.istitle() 返回 True:

# islower() example
string_one = "Artificial Neural Network"
print(string_one.islower())

string_two = string_one.lower()  # converts string to lowercase
print(string_two.islower())

# isupper() example
string_three = string_one.upper() # converts string to uppercase
print(string_three.isupper())

# istitle() example
print(string_one.istitle())

Output:

False
True
True
True

str.endswith(suffix) 返回 True 是以指定后綴結(jié)尾的字符串。如果字符串以指定的前綴開頭,str.startswith(prefix) 返回 True:

sentences = ['Time to master data science', 'I love statistical computing', 'Eat, sleep, code']

# endswith() example
for one_sentence in sentences:
    print(one_sentence.endswith(('science', 'computing', 'Code')))

Output:

True
True
False
# startswith() example
for one_sentence in sentences:
    print(one_sentence.startswith(('Time', 'I ', 'Ea')))

Output:

True
True
True

str.find(substring) 如果子字符串存在于字符串中,則返回最低索引;否則它返回 -1。str.rfind(substring) 返回最高索引。如果找到,str.index(substring) 和 str.rindex(substring) 也分別返回子字符串的最低和最高索引。如果字符串中不存在子字符串,則會(huì)引發(fā) ValueError

string = "programming"

# find() and rfind() examples
print(string.find('m'))
print(string.find('pro'))
print(string.rfind('m'))
print(string.rfind('game'))

# index() and rindex() examples
print(string.index('m'))
print(string.index('pro'))
print(string.rindex('m'))
print(string.rindex('game'))

Output:

6
0
7
-1
6
0
7

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

~\AppData\Local\Temp/ipykernel_11336/3954098241.py in 
     11 print(string.index('pro'))  # Output: 0
     12 print(string.rindex('m'))  # Output: 7
---> 13 print(string.rindex('game'))  # Output: ValueError: substring not found

ValueError: substring not found

str.maketrans(dict_map) 從字典映射創(chuàng)建一個(gè)翻譯表,str.translate(maketrans) 用它們的新值替換翻譯中的元素。例如:

example = "abcde"
mapped = {'a':'1', 'b':'2', 'c':'3', 'd':'4', 'e':'5'}
print(example.translate(example.maketrans(mapped)))

Output:

12345

字符串操作

循環(huán)遍歷一個(gè)字符串

字符串是可迭代的,因此它們支持使用 for 循環(huán)和枚舉的循環(huán)操作:

# For-loop example
word = "bank"
for letter in word:
    print(letter)

Output:

b
a
n
k
# Enumerate example
for idx, value in enumerate(word):
    print(idx, value)

Output:

0 b
1 a
2 n
3 k

字符串和關(guān)系運(yùn)算符

當(dāng)使用關(guān)系運(yùn)算符(>、<、== 等)比較兩個(gè)字符串時(shí),兩個(gè)字符串的元素按其 ASCII 十進(jìn)制數(shù)字逐個(gè)索引進(jìn)行比較。例如:

print('a' > 'b')
print('abc' > 'b')

Output:

False
False

在這兩種情況下,輸出都是 False。關(guān)系運(yùn)算符首先比較兩個(gè)字符串的索引 0 上元素的 ASCII 十進(jìn)制數(shù)。由于 b 大于 a,因此返回 False;在這種情況下,其他元素的 ASCII 十進(jìn)制數(shù)字和字符串的長(zhǎng)度無關(guān)緊要

當(dāng)字符串長(zhǎng)度相同時(shí),它比較從索引 0 開始的每個(gè)元素的 ASCII 十進(jìn)制數(shù),直到找到具有不同 ASCII 十進(jìn)制數(shù)的元素。例如:

print('abd' > 'abc')

Output:

True

檢查字符串的成員資格

in 運(yùn)算符用于檢查子字符串是否是字符串的成員:

print('data' in 'dataquest')
print('gram' in 'programming')

Output:

True
True

檢查字符串成員資格、替換子字符串或匹配模式的另一種方法是使用正則表達(dá)式

import re

substring = 'gram'
string = 'programming'
replacement = '1234'

# Check membership
print(re.search(substring, string))

# Replace string
print(re.sub(substring, replacement, string))

Output:

pro1234ming

字符串格式

f-string 和 str.format() 方法用于格式化字符串。兩者都使用大括號(hào) {} 占位符。例如:

monday, tuesday, wednesday = "Monday", "Tuesday", "Wednesday"

format_string_one = "{} {} {}".format(monday, tuesday, wednesday)
print(format_string_one)

format_string_two = "{2} {1} {0}".format(monday, tuesday, wednesday)
print(format_string_two)

format_string_three = "{one} {two} {three}".format(one=tuesday, two=wednesday, three=monday)
print(format_string_three)

format_string_four = f"{monday} {tuesday} {wednesday}"
print(format_string_four)

Output:

Monday Tuesday Wednesday
Wednesday Tuesday Monday
Tuesday Wednesday Monday
Monday Tuesday Wednesday

f-strings 更具可讀性,并且它們比 str.format() 方法實(shí)現(xiàn)得更快。因此,f-string 是字符串格式化的首選方法

處理引號(hào)和撇號(hào)

撇號(hào) (') 在 Python 中表示一個(gè)字符串。為了讓 Python 知道我們不是在處理字符串,我們必須使用 Python 轉(zhuǎn)義字符 ()。因此撇號(hào)在 Python 中表示為 '。與處理撇號(hào)不同,Python 中有很多處理引號(hào)的方法。它們包括以下內(nèi)容:

# 1. Represent string with single quote (`""`) and quoted statement with double quote (`""`)
quotes_one =  '"Friends don\'t let friends use minibatches larger than 32" - Yann LeCun'
print(quotes_one)

# 2. Represent string with double quote `("")` and quoted statement with escape and double quote `(\"statement\")`
quotes_two =  "\"Friends don\'t let friends use minibatches larger than 32\" - Yann LeCun"
print(quotes_two)

# 3. Represent string with triple quote `("""""")` and quoted statment with double quote ("")
quote_three = """"Friends don\'t let friends use minibatches larger than 32" - Yann LeCun"""
print(quote_three)

Output:

"Friends don't let friends use minibatches larger than 32" - Yann LeCun
"Friends don't let friends use minibatches larger than 32" - Yann LeCun
"Friends don't let friends use minibatches larger than 32" - Yann LeCun
責(zé)任編輯:武曉燕 來源: 蘿卜大雜燴
相關(guān)推薦

2023-08-21 10:28:00

字符串字符Python

2022-05-18 11:35:17

Python字符串

2009-11-24 09:55:44

PHP字符串函數(shù)

2009-12-01 10:38:08

PHP字符串函數(shù)

2010-05-25 15:51:25

MySQL連接字符串

2009-11-17 10:55:02

PHP字符串處理函數(shù)

2010-05-19 14:39:12

MySQL連接字符串

2009-12-18 09:52:40

Ruby字符串處理函數(shù)

2009-09-02 15:56:49

C#格式化字符串

2020-05-14 10:24:41

YAML配置字符串

2023-12-15 10:27:01

暴力匹配算法Python字符串

2021-07-26 00:02:30

Python字符串列表

2011-07-12 14:37:47

ASP.NET鏈接字符串

2009-06-23 14:13:00

Java字符串

2024-04-01 08:41:39

字符串.NET

2010-02-01 16:22:36

Python字符串操作

2010-03-12 10:46:50

Python字符串

2024-05-10 09:26:26

Python字符串

2017-01-17 15:47:18

2010-03-09 16:16:55

Python字符串
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 人碰人操 | 天天爽夜夜爽精品视频婷婷 | 午夜影院在线 | 三级av在线 | 亚洲最色网站 | 欧美激情国产精品 | 美女国内精品自产拍在线播放 | 欧美日韩国产一区二区 | 久久亚洲精品国产精品紫薇 | 国产精品久久国产精品久久 | 99精品欧美一区二区三区 | 精品国产视频 | 亚洲一区 中文字幕 | 亚洲最大的成人网 | 91免费在线视频 | 九九热这里只有精品在线观看 | 欧美一级片 | 欧美成人a∨高清免费观看 欧美日韩中 | 成人夜晚看av | 日韩中文字幕av | 国产精品99久久免费观看 | 日韩有码一区 | 国产98色在线 | 日韩 | 中文在线一区 | 国产精品久久久久久久久久久久冷 | 日韩一区二区在线看 | 亚洲成人午夜电影 | 男女一区二区三区 | 成人av片在线观看 | 亚洲欧美男人天堂 | 久久午夜精品福利一区二区 | 欧美国产中文字幕 | 久久国产精品精品国产色婷婷 | 在线视频a| 国产午夜精品一区二区三区在线观看 | 精品一区二区三区在线视频 | 国产自产c区 | 欧美日韩在线高清 | 亚洲人人| 成人网av| 国产成人精品网站 |