python基础:字符串


本节知识结构

字符串其实也算是一种特殊的有序容器,因为比较特别
所以单独分了出来 L -字符串 - 图1


字符串

定义字符串

  1. # 字符串
  2. 'Hibari'
  3. "Hibari"
  4. # 字符串变量
  5. s = 'Hibari'
  6. s2 = "Hibari"

字符元素

L -字符串 - 图2```python S[index]

参数说明

S 字符串变量

index 整数(表示字符串的下标)

#

返回值

S[] 字符(返回当前元素储存的一个字符)

#

功能说明:

下标从0往大,表示从元素从第一个开始往后数

下标从-1往更小,表示倒着数

#

注意:

虽然单个元素可以单独取出,但并不能修改

  1. ```python
  2. # ================================
  3. # 示例
  4. a = "pyt"
  5. print(a[0])
  6. print(a[1])
  7. print(a[2])
  8. print(" ")
  9. print(a[-1])
  10. print(a[-2])
  11. print(a[-3])
  12. # 输出
  13. p
  14. y
  15. t
  16. t
  17. y
  18. p
  19. # ================================

字符串格式化

  1. # 通过符号'+'可以将字符串连接起来
  1. # ================================
  2. # 示例
  3. a = 'py' +'th' +'on'
  4. print(a)
  5. # 输出
  6. python
  7. # ================================

%s、%d、%f 字符串格式化符号

  1. # 字符串格式化符号:用法和c语言的一样
  2. # %s 整数格式化符号
  3. # %d 整数格式化符号
  4. # %f 浮点格式化符号
  5. #
  6. # 浮点精度:
  7. # %.1f 保留1位小数
  8. # %.2f 以此类推保留2位
  1. # ================================
  2. # 示例
  3. name = 'Hibari'
  4. no = 22
  5. time = 10.16
  6. print("Cname: %s" %name)
  7. print("Number: %d" %no)
  8. print("Ctime: %f" %time)
  9. print("Cname: %s Number: %d Ctime: %.2f" %(name,no,time))
  10. #输出
  11. Cname: Hibari
  12. Number: 22
  13. Ctime: 10.160000
  14. Cname: Hibari Number: 22 Ctime: 10.16
  15. # ================================

format( ) 字符串格式化方法


L -字符串 - 图3```python

示例1

name = ‘Hibari’ no = 22 time = 10.16 print(“Cname: {} Number: {} Ctime: {:.1f}” .format(name,no,time)) print(“ctime: {:.1f}”.format(time))

输出

Cname: Hibari Number: 22 Ctime: 10.2 ctime: 10.2

  1. ```python
  2. # 示例2
  3. S = "{2} {0} {1:.1f}"
  4. print(S.format('Hibari', 3.1415, 'ID:'))
  5. # 输出
  6. ID: Hibari 3.1

字符串内置方法


find( ):——- 查找字符串

L -字符串 - 图4```python

示例

s = “this is not the page” a = s.find(“is”) b = s.find(“is”,3,8) c = s.find(“hibari”) print(a) print(b) print(c)

输出

2 # this中从第3个元素开始有is,所以返回下标2 5 # 从第(3+1)个元素开始查找,第(8+1)个元素结束到第6个元素找到, -1 # 没有找到,返回-1

  1. ---
  2. <a name="JEDrj"></a>
  3. #### count( ):----统计相同字符串
  4. ![](https://cdn.nlark.com/yuque/0/2021/jpeg/2485877/1610907957208-01e520b4-1292-4f57-b6de-1866e246fee1.jpeg)```python
  5. # 示例
  6. s = "this is not the page"
  7. a = s.count("is")
  8. b = s.count("is",3,8)
  9. c = s.count("hibari")
  10. print(a)
  11. print(b)
  12. print(c)
  13. # 输出
  14. 2
  15. 1
  16. 0






replace( ):—替换字符串

L -字符串 - 图5```python

示例

s = “this is not the page” a = s.replace(“is”,”xx”) b = s.replace(“is”,”xx”,1) print(s) print(a) print(b)

输出

this is not the page thxx xx not the page thxx is not the page

  1. ---
  2. <a name="38Cqr"></a>
  3. #### split( ):------分割字符串
  4. ![](https://cdn.nlark.com/yuque/0/2021/jpeg/2485877/1610907957235-ee993cf2-f947-4974-93a4-08f411bb5434.jpeg)```python
  5. # 示例
  6. s = "this is not the web page pagepagepage"
  7. a = s.split()
  8. b = s.split("ag", 2)
  9. print(a)
  10. print(b)
  11. # 输出
  12. ['this', 'is', 'not', 'the', 'web', 'page', 'pagepagepage']
  13. ['this is not the web p', 'e p', 'epagepage']

————————————-


startswith( ):判断起始字符串

endswith( ):-判断后缀字符串

L -字符串 - 图6

  1. # 示例
  2. s = "this is not the web page you are looling for"
  3. print(s.startswith("this"))
  4. print(s.startswith("this",5,))
  5. print(s.startswith("is",5,))
  6. print(s.endswith("for"))
  7. # 输出
  8. True
  9. False
  10. True
  11. True

————————————-


upper( ):——-字符串转大写

lower( ):——-字符串转小写

L -字符串 - 图7```python

示例

s = “Hello World” print(s.upper()) print(“Hello World”.upper()) print(s.lower)

输出

HELLO WORLD HELLO WORLD hello world

  1. <a name="m8Bgs"></a>
  2. #### <br />
  3. <a name="zvCyU"></a>
  4. #### -------------------------
  5. ---
  6. <a name="LZijA"></a>
  7. #### join( ):-------序列 转 字符串
  8. ![](https://cdn.nlark.com/yuque/0/2021/jpeg/2485877/1610907956972-726c8625-27f8-4775-aa0e-a24a357aea00.jpeg)```python
  9. # 示例
  10. s = "/////"
  11. a = ["Hello", "World", "And", "Python"]
  12. print (s.join(a))
  13. # 输出
  14. Hello/////World/////And/////Python

strip( ):———去除开头和结尾的指定字符

L -字符串 - 图8```python

示例

a = “/////Python.py” b = a.strip(“.py”) c = a.strip(“/“) print(b) print(c) print( (a.strip(“/“)).strip(“.py”) )

输出

/////Python Python.py Python

  1. ---
  2. <a name="3JHaz"></a>
  3. #### len( ):--------长度查询
  4. ![](https://cdn.nlark.com/yuque/0/2021/jpeg/2485877/1610907955436-9d592a25-c909-4135-8078-8db14f515d47.jpeg)```python
  5. # 示例
  6. a = (1, "pythong", 3.14)
  7. s = "Hello World"
  8. print(len(a))
  9. print(len(s))
  10. # 输出
  11. 3
  12. 11