日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

python?字符串常用方法超詳細(xì)梳理總結(jié)_python

作者:hacker707 ? 更新時(shí)間: 2022-05-19 編程語言

python基礎(chǔ)之字符串常用方法??????

持續(xù)更新python基礎(chǔ)知識(shí),歡迎各位來訪~??????

字符串是什么?

字符串就是一系列字符。字符串屬于不可變序列,在python中,用引號(hào)包裹的都是字符串,其中引號(hào)可以是單引號(hào),雙引號(hào),也可以是三引號(hào)(單,雙引號(hào)中的字符必須在一行,三引號(hào)中的字符可以分布在多行)

txt = 'hello world' # 使用單引號(hào),字符串內(nèi)容必須在一行
txt1 = "hello python world " # 使用雙引號(hào),字符串內(nèi)容必須在一行
# 使用三引號(hào),字符串內(nèi)容可以分布在多行
txt2 = '''life is short
i use python '''

字符串常用方法

1.find()

定義 find()方法返回該元素最小索引值(找不到返回-1) ??舉個(gè)栗子??返回"python"的最小索引值

txt = "hello python world."
res = txt.find("python")
print(res)

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

6

2.index()

定義 index()方法返回該元素最小索引值(找不到元素會(huì)報(bào)錯(cuò)) ??舉個(gè)栗子??返回"world"的最小索引值

txt = "hello python world."
res = txt.index("world")
print(res)

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

13

3.startswith()

定義 startswith() 方法如果字符串以指定值開頭,返回True,否則返回False ??舉個(gè)栗子??判斷字符串是不是以"hello"開頭

txt = "hello python world."
res = txt.startswith("hello")
print(res)

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

True

4.endswith()

定義 endswith() 方法如果字符串以指定值結(jié)束,返回True,否則返回False ??舉個(gè)栗子??判斷字符串是不是以"hello"結(jié)束

txt = "hello python world."
res = txt.endswith("hello")
print(res)

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

Flase

5.count()

定義 count() 方法返回指定值在字符串中出現(xiàn)的次數(shù)。 ??舉個(gè)栗子??統(tǒng)計(jì)"o"出現(xiàn)次數(shù)

txt = "hello python world."
res = txt.count("o")
print(res)

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

3

6.join()

定義 join() 方法獲取可迭代對(duì)象中的所有項(xiàng)目,并將它們連接為一個(gè)字符串。必須將字符串指定為分隔符 ??舉個(gè)栗子??使用"-"作為分割符,將列表中的所有項(xiàng)連接到字符串中

res = ['h','e','l','l','o']
print('-'.join(res))

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

h-e-l-l-o

7.upper()

定義 upper()方法將字符串全部轉(zhuǎn)為大寫 ??舉個(gè)栗子??將字符串"hello python world"全部轉(zhuǎn)為大寫

tet = "hello python world"
res = txt.upper()
print(res)

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

HELLO WORLD

8.lower()

定義 lower()方法將字符串全部轉(zhuǎn)為小寫 ??舉個(gè)栗子??將字符串"HELLO PYTHON WORLD"全部轉(zhuǎn)為小寫

tet = "HELLO PYTHON WORLD"
res = txt.lower()
print(res)

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

hello python world

9.split()

定義 split()方法以指定字符分割字符串,并返回列表 ??舉個(gè)栗子??以?號(hào)作為分隔符,分割字符串

txt = "hello?python?world"
res = txt.split("?")
print(res)

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

['hello', 'python', 'world']

??擴(kuò)展??分割后打印還是原字符串(字符串是不可變類型,分割操作是復(fù)制一份原字符串,更改的是復(fù)制出來的那一份)

txt = "hello?python?world"
res = txt.split("?")
# 打印分割后的
print(res)
# 打印原字符串
print(txt)
['hello', 'python', 'world']
hello?python?world

10.strip()

定義 strip()方法刪除字符串兩端的空格 ??舉個(gè)栗子??刪除hello兩端的空格

txt = "    hello  "
res = txt.strip()
print(res)

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

hello

11.replace()

定義 replace()方法以指定內(nèi)容替換掉被指定內(nèi)容(默認(rèn)替換全部,可指定替換次數(shù)) ??舉個(gè)栗子??以java替換python

txt = 'hello python world'
res = txt.replace('python','java')
print(res)

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

hello java world

??擴(kuò)展??替換后打印還是原字符串(字符串是不可變類型,替換操作是復(fù)制一份原字符串,更改的是復(fù)制出來的那一份)

txt = 'hello python world'
res = txt.replace('python','java')
# 打印替換后的
print(res)
# 打印原字符串
print(txt)

hello java world
hello python world

以上就是字符串常用的方法整理

原文鏈接:https://blog.csdn.net/xqe777/article/details/123222475

欄目分類
最近更新