python 字符串(12)

在 python變量 文章中我們對python變量做了一個簡單的瞭解,整數/浮點數/bool值相對來講都比較簡單,今天詳細在講解一下關於字符串的內容,字符串俗稱:str

在本文會大量的使用print 和format 函數,如果還有不太熟悉使用的盆友,請先預習:關於python開發中print 函數和format 函數詳細解釋

 

一.字符串運算符

介紹兩個關於python字符串的運算符,”in” 和 “not in”,主要用於檢測字符串中是否存在某個字符或者字符串,如果存在返回True,不存在返回False,直接上代碼演示:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

# !usr/bin/env python

# -*- coding:utf-8 _*-

"""

@Author:何以解憂

@Blog(個人博客地址): shuopython.com

@WeChat Official Account(微信公衆號):猿說python

@Github:www.github.com

@File:string123.py

@Time:2019/9/23 20:45

 

@Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!

"""

 

# 檢測單個字符

str1 = "hello world"

if "h" in str1:

    print("{} 字符串包含 'h'".format(str1))  # 注意單引號和雙引號的配合使用

else:

    print("{} 字符串不包含 'h'".format(str1))

 

# 檢測字符串

if "hello" in str1:

    print("{} 字符串包含 'hello'".format(str1))  # 注意單引號和雙引號的配合使用

else:

    print("{} 字符串不包含 'hello'".format(str1))

 

# 使用 not in

if "hllo" not in str1:

    print("{} 字符串不包含 'hllo'".format(str1))  # 注意單引號和雙引號的配合使用

else:

    print("{} 字符串包含 'hllo'".format(str1))

輸出結果:

1

2

3

hello world 字符串包含 'h'

hello world 字符串包含 'hello'

hello world 字符串不包含 'hllo'

 

二.字符串構造

字符串可以直接拼接,同樣也可以使用format 函數或者 % 符號構造,代碼如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

# 方法一:兩個字符串直接相加拼接在一起

str1 = "hello"

str2 = "world"

str3 = str1 + str2

print("str3 = %s " % str3)

print("{} + {} = {}".format(str1,str2,str3))

 

print("**"*20)

# 方法二:使用format

str4 = "{} {} {}".format("猿說python","python教程","字符串")

print("str4 = %s " % str4)

str5 = "{2} {1} {0}".format("YOU","LOVE","I")  # 注意使用下標索引值,默認重0開始

print("str5 = %s " % str5)

 

print("**"*20)

# 方法三:使用 % 符號 ,% 使用方法與print類似

str6 = "%s%s%s" % ("不積跬步無以至千里",",不積小流無以成江海",

                   ",程序人生的精彩需要堅持不懈地積累!")

print(str6)

輸出結果:

1

2

3

4

5

6

7

str3 = helloworld

hello + world = helloworld

****************************************

str4 = 猿說python python教程 字符串

str5 = I LOVE YOU

****************************************

不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!

 

三.字符串遍歷

可以通過for循環或者while循環遍歷字符串中的每一個字符,在下面代碼中有一個內置函數len()函數,用於獲取字符串長度,代碼如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

str1 = "hello world"

print("%s 字符串總長度:%d" % (str1,len(str1))) # len()獲取字符串長度

 

#方法一:

for i in str1:

    print(i,end="-")  # print 函數默認換行,強制將換行符改爲 '-',可以改爲任意字符

    

print("\n") # "\n" 表示換行

print("*"*20)

 

#方法二:

for i in range(0,len(str1)):

    print(str1[i],end=' ') # 每個字符以空格隔開

 

print("\n") # "\n" 表示換行

print("*"*20)

 

#方法三:

a = 0

while a < len(str1):

    print("str[%d] = %s " % (a,str1[a]))

    a += 1

print("程序結束,退出程序")

輸出結果:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

hello world 字符串總長度:11

h-e-l-l-o- -w-o-r-l-d-

 

********************

h e l l o   w o r l d

 

********************

str[0] = h

str[1] = e

str[2] = l

str[3] = l

str[4] = o

str[5] =  

str[6] = w

str[7] = o

str[8] = r

str[9] = l

str[10] = d

程序結束,退出程序

 

四.字符串截取

字符串中的每一個字符都有一個默認的索引值,從左到右默認重0開始,依次遞增;從右往左默認重-1開始,依次遞增;

字符串索引值

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

str1 = "猿說python"

print(len(str1))            # 內置函數 len() 獲取字符串長度

print(str1)                 # 打印字符串

print(str1[2])              # 獲取字符串中的第二個字符

print(str1[0:2])            # 截取字符串索引值爲0~1的字符,不包括索引值爲2的字符

print(str1[2:5])            # 截取字符串索引值爲2~4的字符,不包括索引值爲5的字符

print(str1[2:-1])           # 截取字符串重索引值爲2開始直到字符串結尾的前一個,-1的索引值表示最後一個

print(str1[2:len(str1)])    # 截取字符串索引值2~8,最後一個字符的索引值爲7,所以剛剛好能截取到字符串末尾

 

# 截取在列表中索引值爲0-4的數據,冒號前面不設置參數,默認重0開始,注意截取並不包括4

print(str1[:4])

# 截取在列表中索引值爲2-末尾的數據,冒號後面不設置參數,默認截取到最後一位數據,注意截取包括最後一位            

print(str1[2:])            

 

print("程序結束,退出程序")

輸出結果:

1

2

3

4

5

6

7

8

9

10

8

猿說python

p

猿說

pyt

pytho

python

猿說py

python

程序結束,退出程序

注意:在上面 print(str1[2:-1]) 改行代碼中,-1 表示最後一位字符串索引,但是截取的範圍並不包括字符串的最後一位。

 

五.字符串替換 – replace()方法

語法:


1

2

3

4

5

6

7

'''

字符串替換方法:替換字符串中指定的內容,並返回新的字符串

    old:字符串中需要被替換的字符或者字符串(舊字符串,原本一直就在字符串)

    new:替換之後的內容(新字符串,添加到字符串代替old的內容)

'''

 

str.replace(old, new)


代碼:


1

2

3

4

5

6

7

str1 = "hello world"

str1 = str1.replace("hello","猿說PYTHON")

print(str1)

 

str1 = "hello world"

str1 = str1.replace("world","python 教程")

print(str1)

輸出內容:

1

2

猿說PYTHON world

hello python 教程

 

六.字符串大小寫

對字符串進行大小寫轉換處理

1

2

3

4

5

str = "www.shuopython.com"

print(str.upper())          # 把所有字符中的小寫字母轉換成大寫字母

print(str.lower())          # 把所有字符中的大寫字母轉換成小寫字母

print(str.capitalize())     # 把第一個字母轉化爲大寫字母,其餘小寫

print(str.title())          # 把每個單詞的第一個字母轉化爲大寫,其餘小寫

輸出結果:

1

2

3

4

WWW.SHUOPYTHON.COM

www.shuopython.com

Www.shuopython.com

Www.Shuopython.Com

關於字符串的函數還有很多,由於篇幅有限,後面的文章我們繼續講解更多關於python字符串相關函數。

 

猜你喜歡:

1.python print 和format詳細使用教程

2.python變量的簡單介紹

 

轉載請註明:猿說Python » python字符串


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