Python中字符串的介紹和使用

Python概念

10年積累的成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先做網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有金塔免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

Python是一種計(jì)算機(jī)程序設(shè)計(jì)語言。是一種面向?qū)ο蟮膭?dòng)態(tài)類型語言,最初被設(shè)計(jì)用于編寫自動(dòng)化腳本(shell),隨著版本的不斷更新和語言新功能的添加,越來越多被用于獨(dú)立的、大型項(xiàng)目的開發(fā)。

字符串概念

  1. 在程序中,文本內(nèi)容用字符串來表示
  2. 字符串是有一系列有序的字符組成,如: 'helloworld'
  3. 字符串和列表,元組一樣,都屬于序列類型'
  4. 可以將字符串看做字符的列表,列表的很多操作對(duì)于字符串也是適用的
  5. 沒有單獨(dú)的字符類型,字符就是指包含一個(gè)元素的字符串 例如: 'a', 'b', 'c'

字符串的創(chuàng)建

'''
使用雙引號(hào)或者單引號(hào)都可以創(chuàng)建字符串
'''
s = 'ok'
print (s,type(s)) # ok <class 'str'>

s1 = "ok1"
print (s1,type(s1)) # ok1 <class 'str'>


'''
使用內(nèi)置函數(shù)str,傳入的可以是數(shù)字,可以是字母,也可以是浮點(diǎn)數(shù),最終該數(shù)據(jù)類型為str類型
'''

s2 = str('abc')
print (s2,type(s2)) # abc <class 'str'>

s3 = str('123')
print (s3,type(s3)) # 123 <class 'str'>

轉(zhuǎn)義字符

1、使用轉(zhuǎn)義字符無法表示的特殊字符

當(dāng)字符串中包含換行、回車、水平制表符、退格等無法直接表示的特殊字符時(shí),該如何表示呢?

    換行: newline,光標(biāo)移動(dòng)到下一行的開頭

    回車:  return,光標(biāo)移動(dòng)到本行開頭

    水平制表符:tab鍵,光標(biāo)移動(dòng)下一組4個(gè)空格開始處

    退格:backspace鍵,回退一個(gè)字符

可以使用如下轉(zhuǎn)義字符

換行:

回車: \r

水平制表符: \t

退格: \b

print('abc\ndef') # abcdef 換行顯示
print('abc\rdef') # def 回車,移動(dòng)到光標(biāo)本行開頭
print('123456\t123\t45') # 123456  123    45 制表符4個(gè)空格,按整體字符長度算
print('abc\bdef') # abdef 退格,刪除了c

2、使用反斜杠"\",作為轉(zhuǎn)義符

例如想直接打印一個(gè)字符串,但想在該字符串中包含單引,雙引等,需要使用\進(jìn)行轉(zhuǎn)義

print('what\'s you name') # what's you name
print('打印一個(gè)雙引號(hào)\"') # 打印一個(gè)雙引號(hào)"
print('打印一個(gè)反斜杠\\') # 打印一個(gè)反斜杠\

3、原始字符串

例如想打印 '\tC:\Program Files' -t為制表符,不想讓-t生效,那么可以使用到原始字符串raw

print('\t:programfiles')  #	:programfiles
print(r'\t:programfiles') #\t:programfiles
print(R'\t:programfiles') #\t:programfiles

4、字符串查操作

   a)  使用index、rindex、find、rfind查找字符串中元素索引

s = '12334567'
print(s.index('3')) # 2 
print(s.rindex('3')) # 3
print(s.find('3'))  # 2 
print(s.rfind('3')) # 3
# 可以指定查找start和stop,例如從索引1開始到5結(jié)束,查找元素3的索引
print(s.index('3',1,5)) # 2
print(s.rfind('3',1,5)) # 3
# 當(dāng)查找的元素不在指定索引中時(shí),index和rindex方法會(huì)拋出value error
# 當(dāng)查找的元素不在指定索引中時(shí),find和rfind方法會(huì)返回-1
print(s.index('9',1,5)) # ValueError: substring not found
print(s.rindex('9',1,5)) # ValueError: substring not found
print(s.find('9',1,5))  # -1 
print(s.rfind('9',1,5)) # -1

 b) 亦可使用查找列表查操作,查找對(duì)應(yīng)索引的元素

s = 'Python'
print(s[3])  # h
print(s[1:4]) # yth
print(s[:-1]) # Pytho
print(s[::-1]) # nohtyP
print(s[:])  # Python
print(s[:2:-1]) # noh

5、字符串的比較

a)  使用 >, <, ==, !=對(duì)字符串進(jìn)行比較

s1 = 'abc'
s2 = 'def'
print(s1 == s2) # False
print(s1[0] < s2[0]) # True
print(s1[1] > s2[0]) # False
print(s1 != s2) # True

b) 字符串也可以適用is,==是比較相等性,is是比較同一性,但是對(duì)于字符串來說,python重復(fù)進(jìn)行調(diào)用

a = b = '123'
c = '123'
print (a is b) #True
print (a == c) #True
print (a is c) #True
print(id(a),id(c))  #139917133452656 139917133452656


6、字符串的反轉(zhuǎn)

使用內(nèi)置函數(shù)reversed對(duì)字符串進(jìn)行反轉(zhuǎn)

s = 'hello world'
print(list(reversed(s)))  # ['d', 'l', 'r', 'o', 'w', ' ', 'o', 'l', 'l', 'e', 'h']

7、字符串的排序

使用內(nèi)置函數(shù)sorted對(duì)字符串進(jìn)行排序

s = 'EdfaCb'
# 可以指定排序規(guī)則,例如先轉(zhuǎn)換成小寫,然后進(jìn)行排序,或者排序完對(duì)字符串進(jìn)行反轉(zhuǎn)
# 不指定規(guī)則,則按照ord()的方式排序
print(sorted(s,key = str.lower)) # ['a', 'b', 'C', 'd', 'E', 'f']
print(sorted(s,reverse = True)) # ['f', 'd', 'b', 'a', 'E', 'C']
print(sorted(s)) # ['C', 'E', 'a', 'b', 'd', 'f']

8、字符串的大小寫轉(zhuǎn)換

a) upper 將所有字符轉(zhuǎn)換成大寫

b) lower 將所有字符轉(zhuǎn)換成小寫

c) swapcase 把所有小寫轉(zhuǎn)換成大寫,大寫轉(zhuǎn)小寫

d) title 把每個(gè)單詞的開頭轉(zhuǎn)換成大寫

s = 'java PytHon Shell'
print(s.lower()) #java python shell
print(s.upper()) # JAVA PYTHON SHELL
print(s.swapcase()) # JAVA pYThON sHELL
print(s.title()) # Java Python Shell

9、字符串的對(duì)齊

a) center 中心對(duì)齊

b) rjust 右對(duì)齊

c) ljust 左對(duì)齊

以上方法可以指定兩個(gè)參數(shù),第一個(gè)參數(shù)是寬度,第二個(gè)參數(shù)是對(duì)齊符號(hào),不指定第二個(gè)參數(shù)默認(rèn)是空格。

d) zfill:右對(duì)齊,左邊用0填充

該方法值接收一個(gè)參數(shù),用于指定字符的寬度,如果指定的字符寬度小于字符串本身,那么返回字符串本身

s = 'hello world'
print(s.center(20,'*')) # ****hello world*****
print(s.ljust(18,'^')) # hello world^^^^^^^
print(s.rjust(18,'$')) # $$$$$$$hello world
print(s.zfill(15)) # 0000hello world

10、字符串的替換

調(diào)用replace方法,對(duì)字符串進(jìn)行替換,str.replace('old','new','replace_num')

s = 'hi hi hi hello'
#將hi替換為hello,最大替換個(gè)數(shù)為2個(gè),不指定替換個(gè)數(shù)默認(rèn)為全部
print(s.replace('hi','hello',2)) # hello hello hi hello

11、去除字符串的前導(dǎo)符和后導(dǎo)符

調(diào)用方法lstrip、rstrip、strip對(duì)字符串前導(dǎo)符和后導(dǎo)符進(jìn)行去除

s = '****hello world^^^^^'
print(s.lstrip('*'))  # hello world^^^^^
print(s.rstrip('^'))  # ****hello world
print(s.strip('*^'))  # hello world

網(wǎng)站標(biāo)題:Python中字符串的介紹和使用
路徑分享:http://muchs.cn/article8/gedgop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、微信公眾號(hào)、企業(yè)網(wǎng)站制作、動(dòng)態(tài)網(wǎng)站、網(wǎng)站設(shè)計(jì)公司、標(biāo)簽優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化