自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

盤點(diǎn)Python字符串常見的16種操作方法

開發(fā) 后端
本文詳細(xì)的講解了Python基礎(chǔ) ( 字符串 )。介紹了有關(guān)字符串,切片的操作。下標(biāo)索引。以及在實(shí)際操作中會(huì)遇到的問題,提供了解決方案。希望可以幫助你更好的學(xué)習(xí)Python。

[[409765]]

大家好,我是Go進(jìn)階者,上篇文章給大家介紹了Python字符串,今天給大家分享一些Python字符串的常用操作,一起來看看吧~

一、常用操作

以字符串

  1. 'lstr = 'welcome to Beijing Museumitcpps fdsfs' 

為例,介紹字符常見的操作。

<1> find

檢測 str 是否包含在 lstr中,如果是返回開始的索引值,否則返回-1。

語法:

  1. lstr.find(str, start=0, end=len(lstr)) 

例:

  1. lstr = 'welcome to Beijing Museumitcpps fdsfs' 
  2. print(lstr.find("Museum")) 
  3.  
  4. print(lstr.find("dada")) 

運(yùn)行結(jié)果:

<2> index

跟find()方法一樣,只不過如果str不在 lstr中會(huì)報(bào)一個(gè)異常。

語法:

  1. lstr.index(str, start=0, end=len(lstr)) 

例:

  1. lstr = 'welcome to Beijing Museumitcpps fdsfs' 
  2.  
  3. print(lstr.index("dada")) 

運(yùn)行結(jié)果:

<3> count

返回 str在start和end之間 在 lstr里面出現(xiàn)的次數(shù)

語法:

  1. lstr.count(str, start=0, end=len(lstr)) 

例:

  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2.  
  3. print(lstr.count("s")) 

運(yùn)行結(jié)果:

<4> replace

把 lstr 中的 str1 替換成 str2,如果 count 指定,則替換不超過 count 次.

  1. 1str.replace(str1, str2,  1str.count(str1)) 

例:

  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2.  
  3. print(lstr.replace("s""ttennd")) 

運(yùn)行結(jié)果:

<5> split

以 str 為分隔符切片 lstr,如果 maxsplit有指定值,則僅分隔 maxsplit 個(gè)子字符串

  1. 1str.split(str=" ", 2) 

例:

  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2.  
  3. print(lstr.split("to", 5)) 

運(yùn)行結(jié)果:

<6> capitalize

把字符串的第一個(gè)字符大寫。

  1. lstr.capitalize() 

例:

  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2.  
  3. print(lstr.capitalize()) 

運(yùn)行結(jié)果:

<7> title

把字符串的每個(gè)單詞首字母大寫。

  1. >>> a = "hello itcast" 
  2. >>> a.title() 
  3. 'Hello Itcast' #運(yùn)行結(jié)果 

<8> startswith

檢查字符串是否是以 obj 開頭, 是則返回 True,否則返回 False

  1. 1str.startswith(obj) 

例:

  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2.  
  3. print(lstr.startswith('we')) 

運(yùn)行結(jié)果:

<9> endswith

檢查字符串是否以obj結(jié)束,如果是返回True,否則返回 False.

  1. 1str.endswith(obj) 

例:

  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2.  
  3. print(lstr.endswith('hfs')) 

運(yùn)行結(jié)果:

<10> lower

轉(zhuǎn)換 lstr 中所有大寫字符為小寫

  1. 1str.lower() 

例:

  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2.  
  3. print(lstr.lower()) 

運(yùn)行結(jié)果:

<11> upper

轉(zhuǎn)換 lstr 中的小寫字母為大寫

  1. 1str.upper() 

例:

  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2.  
  3. print(lstr.upper()) 

運(yùn)行結(jié)果:

<12> strip

刪除lstr字符串兩端的空白字符。

  1. >>> a = "\n\t itcast \t\n" 
  2. >>> a.strip() 
  3. 'itcast'  #運(yùn)行結(jié)果 

<13> rfind

類似于 find()函數(shù),不過是從右邊開始查找。

  1. 1str.rfind(str, start=0,end=len(1str) ) 

例:

  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2. print(lstr.rfind('eijing')) 

運(yùn)行結(jié)果:

<14> rindex

類似于 index(),不過是從右邊開始。

  1. 1str.rindex( str, start=0,end=len(1str)) 

例:

  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2. print(lstr.rindex('eijing')) 

運(yùn)行結(jié)果:

<15> partition

把lstr以str分割成三部分,str前,str和str后。

  1. 1str.partition(str) 

例:

  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2. print(lstr.partition('eijing')) 

運(yùn)行結(jié)果:

<16> join

mystr 中每個(gè)字符后面插入str,構(gòu)造出一個(gè)新的字符串。

  1. lstr = 'welcome to Beijing Museumitcpps  fdsfs' 
  2. str='233' 
  3. lstr.join(str) 
  4. li=["my","name","is","LY"
  5. print(str.join(li)) 

運(yùn)行結(jié)果:

二、總結(jié)

本文詳細(xì)的講解了Python基礎(chǔ) ( 字符串 )。介紹了有關(guān)字符串,切片的操作。下標(biāo)索引。以及在實(shí)際操作中會(huì)遇到的問題,提供了解決方案。希望可以幫助你更好的學(xué)習(xí)Python。

 

責(zé)任編輯:姜華 來源: Go語言進(jìn)階學(xué)習(xí)
相關(guān)推薦

2010-03-11 09:56:57

Python字符串操作

2024-10-30 16:49:00

Python字符串

2009-08-28 15:25:38

C#線程操作

2015-04-08 10:27:43

JavaScript字符串操作函數(shù)

2010-02-01 09:40:08

Python操作

2020-06-28 08:26:41

Python開發(fā)工具

2021-04-17 10:05:57

Python字符串Python基礎(chǔ)

2023-04-17 19:23:10

字符串Bash

2024-10-23 09:00:00

數(shù)據(jù)分析Pandas

2010-03-05 13:48:24

Python for

2010-03-04 09:58:32

安裝Python

2010-03-15 15:18:23

Python運(yùn)行

2023-08-25 16:37:08

Pandas測試

2024-05-10 09:26:26

Python字符串

2010-02-01 16:22:36

Python字符串操作

2010-09-02 10:02:17

PHP

2020-08-01 16:19:13

JavaScript字符串開發(fā)

2021-05-18 09:08:18

字符串子串對(duì)象

2023-12-05 08:02:51

JavaScript字符串功能

2025-04-27 10:02:50

JavaScript前端開發(fā)
點(diǎn)贊
收藏

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