欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python入門教程(九)Python字符串介紹

 更新時間:2023年04月11日 08:55:33   作者:輕松學(xué)Python  
這篇文章主要介紹了Python入門教程(九)Python字符串,Python是一門非常強大好用的語言,也有著易上手的特性,本文為入門教程,需要的朋友可以參考下

字符串字面量

python 中的字符串字面量由單引號或雙引號括起。

‘hello’ 等同于 “hello”。

可以使用 print() 函數(shù)顯示字符串字面量:

實例

print("Hello")
print('Hello')

運行實例

用字符串向變量賦值

通過使用變量名稱后跟等號和字符串,可以把字符串賦值給變量:

實例

a = "Hello"
print(a)

運行實例

多行字符串

可以使用三個引號將多行字符串賦值給變量:

實例

可以使用三個雙引號:

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)

運行實例

或三個單引號:

實例

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)

運行實例

注釋:在結(jié)果中,換行符插入與代碼中相同的位置。

字符串是數(shù)組

像許多其他流行的編程語言一樣,Python 中的字符串是表示 unicode 字符的字節(jié)數(shù)組。

但是,Python 沒有字符數(shù)據(jù)類型,單個字符就是長度為 1 的字符串。

方括號可用于訪問字符串的元素。

實例

獲取位置 1 處的字符(請記住第一個字符的位置為 0):

a = "Hello, World!"
print(a[1])

運行實例

裁切

可以使用裁切語法返回一定范圍的字符。

指定開始索引和結(jié)束索引,以冒號分隔,以返回字符串的一部分。

實例

獲取從位置 2 到位置 5(不包括)的字符:

b = "Hello, World!"
print(b[2:5])

運行實例

負的索引

使用負索引從字符串末尾開始切片:

實例

獲取從位置 5 到位置 1 的字符,從字符串末尾開始計數(shù):

b = "Hello, World!"
print(b[-5:-2])

運行實例

字符串長度

如需獲取字符串的長度,請使用 len() 函數(shù)。

實例

len() 函數(shù)返回字符串的長度:

a = "Hello, World!"
print(len(a))

運行實例

字符串方法

Python 有一組可用于字符串的內(nèi)置方法。

實例

strip() 方法刪除開頭和結(jié)尾的空白字符:

a = " Hello, World! "
print(a.strip()) # returns "Hello, World!"

運行實例

在這里插入圖片描述

實例

lower() 返回小寫的字符串:

a = "Hello, World!"
print(a.lower())

運行實例

實例

upper() 方法返回大寫的字符串:

a = "Hello, World!"
print(a.upper())

運行實例

實例

replace() 用另一段字符串來替換字符串:

a = "Hello, World!"
print(a.replace("World", "Kitty"))

運行實例

實例

split() 方法在找到分隔符的實例時將字符串拆分為子字符串:

a = "Hello, World!"
print(a.split(",")) # returns ['Hello', ' World!']

運行實例

請使用我們的字符串方法參考手冊,學(xué)習更多的字符串方法。

檢查字符串

如需檢查字符串中是否存在特定短語或字符,我們可以使用 in 或 not in 關(guān)鍵字。

實例

檢查以下文本中是否存在短語 “ina”:

txt = "China is a great country"
x = "ina" in txt
print(x)

運行實例

實例

檢查以下文本中是否沒有短語 “ina”:

txt = "China is a great country"
x = "ain" not in txt
print(x) 

運行實例

字符串級聯(lián)(串聯(lián))

如需串聯(lián)或組合兩個字符串,您可以使用 + 運算符。

實例

將變量 a 與變量 b 合并到變量 c 中:

a = "Hello"
b = "World"
c = a + b
print(c)

運行實例

實例

在它們之間添加一個空格:

a = "Hello"
b = "World"
c = a + " " + b
print(c)

運行實例

字符串格式

正如在 Python 變量一章中所學(xué)到的,我們不能像這樣組合字符串和數(shù)字:

實例

age = 63
txt = "My name is Bill, I am " + age
print(txt)

運行實例

但是我們可以使用 format() 方法組合字符串和數(shù)字!

format() 方法接受傳遞的參數(shù),格式化它們,并將它們放在占位符 {} 所在的字符串中:

實例

使用 format() 方法將數(shù)字插入字符串:

age = 63 
txt = "My name is Bill, and I am {}"
print(txt.format(age))

運行實例

format() 方法接受不限數(shù)量的參數(shù),并放在各自的占位符中:

實例

quantity = 3
itemno = 567
price = 49.95
myorder = "I want {} pieces of item {} for {} dollars."
print(myorder.format(quantity, itemno, price))

運行實例

您可以使用索引號 {0} 來確保參數(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))

運行實例

字符串方法

Python 有一組可以在字符串上使用的內(nèi)建方法。

注釋:所有字符串方法都返回新值。它們不會更改原始字符串。

注釋:所有字符串方法都返回新值。它們不會更改原始字符串。

到此這篇關(guān)于Python入門教程(九)Python字符串介紹的文章就介紹到這了,更多相關(guān)Python字符串內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論