Python入門教程(九)Python字符串介紹
字符串字面量
python 中的字符串字面量由單引號(hào)或雙引號(hào)括起。
‘hello’ 等同于 “hello”。
可以使用 print() 函數(shù)顯示字符串字面量:
實(shí)例
print("Hello")
print('Hello')運(yùn)行實(shí)例

用字符串向變量賦值
通過使用變量名稱后跟等號(hào)和字符串,可以把字符串賦值給變量:
實(shí)例
a = "Hello" print(a)
運(yùn)行實(shí)例

多行字符串
可以使用三個(gè)引號(hào)將多行字符串賦值給變量:
實(shí)例
可以使用三個(gè)雙引號(hào):
a = """Python is a widely used general-purpose, high level programming language. It was initially designed by Guido van Rossum in 1991 and developed by Python Software Foundation. It was mainly developed for emphasis on code readability, and its syntax allows programmers to express concepts in fewer lines of code.""" print(a)
運(yùn)行實(shí)例

或三個(gè)單引號(hào):
實(shí)例
a = '''Python is a widely used general-purpose, high level programming language. It was initially designed by Guido van Rossum in 1991 and developed by Python Software Foundation. It was mainly developed for emphasis on code readability, and its syntax allows programmers to express concepts in fewer lines of code.''' print(a)
運(yùn)行實(shí)例

注釋:在結(jié)果中,換行符插入與代碼中相同的位置。
字符串是數(shù)組
像許多其他流行的編程語言一樣,Python 中的字符串是表示 unicode 字符的字節(jié)數(shù)組。
但是,Python 沒有字符數(shù)據(jù)類型,單個(gè)字符就是長度為 1 的字符串。
方括號(hào)可用于訪問字符串的元素。
實(shí)例
獲取位置 1 處的字符(請(qǐng)記住第一個(gè)字符的位置為 0):
a = "Hello, World!" print(a[1])
運(yùn)行實(shí)例

裁切
可以使用裁切語法返回一定范圍的字符。
指定開始索引和結(jié)束索引,以冒號(hào)分隔,以返回字符串的一部分。
實(shí)例
獲取從位置 2 到位置 5(不包括)的字符:
b = "Hello, World!" print(b[2:5])
運(yùn)行實(shí)例

負(fù)的索引
使用負(fù)索引從字符串末尾開始切片:
實(shí)例
獲取從位置 5 到位置 1 的字符,從字符串末尾開始計(jì)數(shù):
b = "Hello, World!" print(b[-5:-2])
運(yùn)行實(shí)例

字符串長度
如需獲取字符串的長度,請(qǐng)使用 len() 函數(shù)。
實(shí)例
len() 函數(shù)返回字符串的長度:
a = "Hello, World!" print(len(a))
運(yùn)行實(shí)例

字符串方法
Python 有一組可用于字符串的內(nèi)置方法。
實(shí)例
strip() 方法刪除開頭和結(jié)尾的空白字符:
a = " Hello, World! " print(a.strip()) # returns "Hello, World!"
運(yùn)行實(shí)例

實(shí)例
lower() 返回小寫的字符串:
a = "Hello, World!" print(a.lower())
運(yùn)行實(shí)例

實(shí)例
upper() 方法返回大寫的字符串:
a = "Hello, World!" print(a.upper())
運(yùn)行實(shí)例

實(shí)例
replace() 用另一段字符串來替換字符串:
a = "Hello, World!"
print(a.replace("World", "Kitty"))
運(yùn)行實(shí)例

實(shí)例
split() 方法在找到分隔符的實(shí)例時(shí)將字符串拆分為子字符串:
a = "Hello, World!"
print(a.split(",")) # returns ['Hello', ' World!']
運(yùn)行實(shí)例

請(qǐng)使用我們的字符串方法參考手冊(cè),學(xué)習(xí)更多的字符串方法。
檢查字符串
如需檢查字符串中是否存在特定短語或字符,我們可以使用 in 或 not in 關(guān)鍵字。
實(shí)例
檢查以下文本中是否存在短語 “ina”:
txt = "China is a great country" x = "ina" in txt print(x)
運(yùn)行實(shí)例

實(shí)例
檢查以下文本中是否沒有短語 “ina”:
txt = "China is a great country" x = "ain" not in txt print(x)
運(yùn)行實(shí)例

字符串級(jí)聯(lián)(串聯(lián))
如需串聯(lián)或組合兩個(gè)字符串,您可以使用 + 運(yùn)算符。
實(shí)例
將變量 a 與變量 b 合并到變量 c 中:
a = "Hello" b = "World" c = a + b print(c)
運(yùn)行實(shí)例

實(shí)例
在它們之間添加一個(gè)空格:
a = "Hello" b = "World" c = a + " " + b print(c)
運(yùn)行實(shí)例

字符串格式
正如在 Python 變量一章中所學(xué)到的,我們不能像這樣組合字符串和數(shù)字:
實(shí)例
age = 63 txt = "My name is Bill, I am " + age print(txt)
運(yùn)行實(shí)例

但是我們可以使用 format() 方法組合字符串和數(shù)字!
format() 方法接受傳遞的參數(shù),格式化它們,并將它們放在占位符 {} 所在的字符串中:
實(shí)例
使用 format() 方法將數(shù)字插入字符串:
age = 63
txt = "My name is Bill, and I am {}"
print(txt.format(age))
運(yùn)行實(shí)例

format() 方法接受不限數(shù)量的參數(shù),并放在各自的占位符中:
實(shí)例
quantity = 3
itemno = 567
price = 49.95
myorder = "I want {} pieces of item {} for {} dollars."
print(myorder.format(quantity, itemno, price))
運(yùn)行實(shí)例

您可以使用索引號(hào) {0} 來確保參數(shù)被放在正確的占位符中:
實(shí)例
quantity = 3
itemno = 567
price = 49.95
myorder = "I want to pay {2} dollars for {0} pieces of item {1}."
print(myorder.format(quantity, itemno, price))
運(yùn)行實(shí)例

字符串方法
Python 有一組可以在字符串上使用的內(nèi)建方法。
注釋:所有字符串方法都返回新值。它們不會(huì)更改原始字符串。

注釋:所有字符串方法都返回新值。它們不會(huì)更改原始字符串。
到此這篇關(guān)于Python入門教程(九)Python字符串介紹的文章就介紹到這了,更多相關(guān)Python字符串內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python 自動(dòng)化偷懶的四個(gè)實(shí)用操作
這篇文章主要介紹了python 自動(dòng)化偷懶的四個(gè)實(shí)用操作,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-04-04
Python+Pygame實(shí)現(xiàn)簡單的射擊小游戲
要說什么游戲能夠獲得大家的喜愛,唯射擊游戲莫屬!本文將利用Python和Pygame庫制作一個(gè)簡單的射擊小游戲,感興趣的小伙伴可以了解一下2022-04-04
使用Python實(shí)現(xiàn)一個(gè)優(yōu)雅的異步定時(shí)器
在 Python 中實(shí)現(xiàn)定時(shí)器功能是一個(gè)常見需求,尤其是在需要周期性執(zhí)行任務(wù)的場(chǎng)景下,本文給大家介紹了基于 asyncio 和 threading 模塊,可擴(kuò)展的異步定時(shí)器實(shí)現(xiàn),需要的朋友可以參考下2025-04-04
Python實(shí)現(xiàn)將絕對(duì)URL替換成相對(duì)URL的方法
這篇文章主要介紹了Python實(shí)現(xiàn)將絕對(duì)URL替換成相對(duì)URL的方法,涉及Python字符串操作及正則匹配的相關(guān)技巧,需要的朋友可以參考下2015-06-06
python應(yīng)用Axes3D繪圖(批量梯度下降算法)
這篇文章主要為大家詳細(xì)介紹了python應(yīng)用Axes3D繪圖,批量梯度下降算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03

