python學習總結 基礎篇 字符串詳解

Python

字符串

字符串的定義

Python無需專門定義數據類型,直接給變量賦值一個字符串
字符串可以包含在單引號或雙引號之間

>>> str1='abcd'
>>> str1
'abcd'
>>> 
>>> str2="abcdefg"
>>> str2
'abcdefg'

單雙引號都可以用來定義字符串好處在於字符串種還可以包含單雙引號,只要最外層的引號和內層的引號不相同即可

>>> str3="She's a girl!"
>>> print(str3)
She's a girl!

索引

索引運算符** [ i ],i爲下標 **有正序和負序的索引方式
在這裏插入圖片描述
注意,正序第一個的索引爲0,倒序第一個索引爲-1
代碼示例:

>>> str2="abcdefg"
>>> str2
'abcdefg'
>>> str2[1]
'b'
>>> str2[-1]
'g'
>>> 

切片運算

切片就是將一個字符串中的某一部分切取出來,切片運算符[i:j:k] 。 i、j爲字符串片段的起始下標,k爲步長

>>> test = "Hello World"
>>> test[1:7]
'ello W'
>>> test[1:7:2]
'el '
>>> test[0:]
'Hello World'
>>> test[:5]
'Hello'
>>> test[::-1]
'dlroW olleH'
>>> test[5:1:1]
''
>>> test[5:1:-1]
' oll'

注意切片的區間爲左閉右開,例如test[1:7]是從索引爲1的取到索引爲6的,而test[5:1:-1]是索引爲5的倒着輸出到索引爲2的。
特別注意test[::-1] 和test[:5]切片。

常用轉移字符

在這裏插入圖片描述

字符串運算

加號運算 字符串連接

加號運算符就是將幾個不同的字符串連接成一個字符串

>>> 'aaa'+'bbb'
'aaabbb'
>>> str1="Hello "
>>> str2="World!"
>>> str1 + str2
'Hello World!'
>>> str3="Again"
>>> str1 + str2 + str3
'Hello World!Again'
>>> 

星號* 字符串複製

字符串*n就是將一個字符串複製n次,併合成一個字符串

>>> "aaa "*3
'aaa aaa aaa '
>>> str1='aaa'
>>> str1*3
'aaaaaaaaa'
>>> 

in 運算符

in運算符用來判斷某個字符是否字符串裏,返回爲bool值

>>> 'a' in "aaa"
True
>>> test = "Hello World"
>>> if 'H' in test :
	print(" 'H' in test ")

 'H' in test 

not in運算符

not in運算符用來判斷某個字符是否不在字符串裏,返回爲bool值

>>> test = "Hello World"
>>> if 'A' not in test:
	print("'A' not in test")

'A' not in test

常用字符串長度

len()函數

len()函數用來計算一個字符串的長度

>>> test = "Hello World"
>>> len(test)
11

ord()函數 chr()函數

ord()函數用來顯示字符對應的ASCII整數
chr()函數用來將給定的ASCII整數轉換爲對應的字符

>>> a = 'a'
>>> ord(a)
97
>>> chr(97)
'a'

str()函數

str()函數將其他數據類型轉換成字符串類型

>>> str(66666666666)
'66666666666'
>>> str(6666.66666666666666)
'6666.666666666667'
>>> str(6666.6666666666666666666666)
'6666.666666666667'

字符串常用方法

注意在Python中函數與方法的區別,函數是直接使用的,如len(“aaaa”),方法是變量 . 方法名()

upper() lower()

upper()方法將小寫轉換成大寫。
lower()方法將大寫轉換成小寫。

>>> test='hello world'
>>> test.upper()
'HELLO WORLD'
>>> upp='HELLO WORLD'
>>> upp.lower()
'hello world'

find()

find()方法用來在字符串內部定位一個字串,即查找,返回值爲下標索引

>>> test='hello world'
>>> test.find('o')
4

replace()

replace()方法用第二個子串替代第一個子串,這個方法是針對某一個字符串進行操作,將該字符串中的一個子串進行替代。

>>> test='hello world'
>>> test.replace('wor','o')
'hello old'
>>> test
'hello world'

由代碼可知這個方法並不改變原字符串,而僅僅是改變輸出結果。

>>> test='hello world lll'
>>> test.replace('l','x')
'hexxo worxd xxx'

由代碼示例可知replace()方法會對字符串中所有符合要求的子串進行替代。

strip()

strip()方法用來消除字符串兩端的空格和符合

>>> test='    hello world    '
>>> test.strip()
'hello world'

format()格式化輸出

看代碼:

>>> "{0}的年齡是{1}".format("張三",20)
'張三的年齡是20'
>>> "{name}的年齡是{age}".format(age=38,name="李四")
'李四的年齡是38'

代碼中{0},{1}爲佔位符,就是爲格式化輸出預留的位置。 format()中的數據根據前面的佔位符自動匹配相應位置輸出。這種方式必須按順序擺放要格式化輸出的各值。
第二種方法直接傳遞變量名,可以不按順序擺放。

isalpha()

isalpha()方法在字符串非空且只有字母時爲真。

>>> test="hello"
>>> test.isalpha()
True
>>> test="hello "
>>> test.isalpha()
False
>>> test="hello1"
>>> test.isalpha()
False

isdigit()

isalpha()方法在字符串非空且只有數字時爲真。

>>> test="123"
>>> test.isdigit()
True
>>> test="123 "
>>> test.isdigit()
False
>>> test="123a"
>>> test.isdigit()
False

split()

split()方法將一個字符串根據空格切分成若干的子串。 簡單的說就是分成一個一個的單詞。所有由多少個就要用多少個變量來存放這些子串,也可以用列表來存放。

>>> test="This is my first Python"
>>> field1,field2,field3,field4,field5=test.split()
>>> field1
'This'
>>> field2
'is'
>>> field3
'my'
>>> field4
'first'
>>> field5
'Python'
>>> test
'This is my first Python'

當單詞數大於或小於所定義的變量數時,會報錯
在這裏插入圖片描述

join()

join()方法有點意思,看代碼體會:

>>> str1="Hello"
>>> str2="World"
>>> str1.join(str2)
'WHellooHellorHellolHellod'
>>> str1="aa"
>>> str2="bb"
>>> str1.join(str2)
'baab'
>>> str2.join(str1)
'abba'
>>> str1="aa"
>>> str2="bcdefg"
>>> str1.join(str2)
'baacaadaaeaafaag'

這個就是先將str2拆開,再將str1插入到str2的每一個字符之間

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章