1. 程式人生 > >python基礎之字符串

python基礎之字符串

int AMM ring nds sub 對象 itl 對齊 常用

字符串的聲明:

1 mystr = Hello world
2 
3 mystr = "Hello world"
4 
5 ‘‘‘
6 小結: 雙引號或者單引號中的數據,就是字符串
7 ‘‘‘

字符串的輸出:

 1 name = FuChongjun
 2 position = programmer
 3 address=Beijing
 4 
 5 print(------------------------------)
 6 print(第一種輸出方式)
 7 print(姓名:+name)
 8 print(職位:+position)
 9 print
(地址:+address) 10 print(------------------------------) 11 12 print(第二種輸出方式) 13 print(姓名:%s%name) 14 print(職位:%s%position) 15 print(地址:%s%address) 16 print(------------------------------) 17 18 print(第三種輸出方式) 19 print(姓名:{}.format(name)) 20 print(職位:{}.format(position)) 21 print(
地址:{}.format(address)) 22 print(------------------------------) 23 24 print(第四種輸出方式) 25 print(姓名:{name}.format(name=name)) 26 print(職位:{position}.format(position=position)) 27 print(地址:{address}.format(address=address)) 28 print(------------------------------) 29 30 結果: 31 ------------------------------ 32
第一種輸出方式 33 姓名:FuChongjun 34 職位:programmer 35 地址:Beijing 36 ------------------------------ 37 第二種輸出方式 38 姓名:FuChongjun 39 職位:programmer 40 地址:Beijing 41 ------------------------------ 42 第三種輸出方式 43 姓名:FuChongjun 44 職位:programmer 45 地址:Beijing 46 ------------------------------ 47 第四種輸出方式 48 姓名:FuChongjun 49 職位:programmer 50 地址:Beijing 51 ------------------------------

字符串的輸入:

username = input(請輸入用戶名:\n).strip()
password = input(請輸入密碼:\n).strip()
print(-----------------------)
print(用戶名:{}\n密碼:{}.format(username,password))
print(-----------------------)

結果:
-----------------------
用戶名:pisa
密碼:123
-----------------------

下標和切片:

切片是指對操作的對象截取其中一部分的操作。字符串、列表、元組都支持切片操作。

 1 ‘‘‘
 2 切片的語法:[起始:結束:步長]
 3 選取的區間為左閉右開區間 [a,b)
 4 ‘‘‘
 5 name = abcdf
 6 
 7 print(name[0:3]) #取出從索引為0到索引為3,但不包括3.結果abc
 8 
 9 print(name[0:-1]) #取出從索引0開始到索引倒數第一個為止,但是不包含倒數第一個.結果:abcd
10 
11 print(name[0:3:2])#取出從索引0開始,每隔一個取一個,到索引為3結束,不包括索引3 結果:ac
12 
13 print(name[0:-1:2])#取出從索引0開始,每隔1個取一個,到倒數第一個結束,不包括倒數第一個。結果:ac
14 
15 print(name[3:1:-1])#取出從索引1開始,不包括1,到索引為3結束,包括索引3,並從右到左返回。結果:dc
16 
17 print(name[::-1]) #字符串反轉

字符串常用的方法:

  1 mystr = hello world
  2 
  3 ‘‘‘
  4 str.find(str, beg=0, end=len(string))
  5     str -- 指定檢索的字符串
  6     beg -- 開始索引,默認為0。
  7     end -- 結束索引,默認為字符串的長度。
  8     如果包含子字符串返回開始的索引值,否則返回 -1
  9 ‘‘‘
 10 print(mystr.find(d)) # 10
 11 
 12 
 13 ‘‘‘
 14 查詢 d 在 mystr中的第幾個為止
 15 str.index(str, beg=0, end=len(string))
 16 
 17     str -- 指定檢索的字符串
 18     beg -- 開始索引,默認為0。
 19     end -- 結束索引,默認為字符串的長度。
 20     如果包含子字符串返回開始的索引值,否則拋出異常。
 21 ‘‘‘
 22 try:
 23     print(mystr.index(z))
 24 except:
 25     print(z不在mystr中)  #這是結果
 26 
 27 ‘‘‘
 28 查詢 a 在 mystr中出現多少次
 29 str.count(sub, start= 0,end=len(string))
 30     sub -- 搜索的子字符串
 31     start -- 字符串開始搜索的位置。默認為第一個字符,第一個字符索引值為0。
 32     end -- 字符串中結束搜索的位置。字符中第一個字符的索引為 0。默認為字符串的最後一個位置
 33 ‘‘‘
 34 print(mystr.count(a)) #0
 35 
 36 ‘‘‘
 37 把 mystr 中的 str1 替換成 str2,如果 count 指定,則替換不超過 count 次
 38 str.replace(old, new[, max])
 39     old -- 將被替換的子字符串。
 40     new -- 新字符串,用於替換old子字符串。
 41     max -- 可選字符串, 替換不超過 max 次
 42     返回字符串中的 old(舊字符串) 替換成 new(新字符串)後生成的新字符串,如果指定第三個參數max,則替換不超過 max 次。
 43 ‘‘‘
 44 print(mystr.replace(o,a,2)) # hella warld
 45 
 46 ‘‘‘
 47 Python split() 通過指定分隔符對字符串進行切片,如果參數 num 有指定值,則僅分隔 num 個子字符串
 48 str.split(str="", num=string.count(str)).
 49     str -- 分隔符,默認為所有的空字符,包括空格、換行(\n)、制表符(\t)等。
 50     num -- 分割次數。
 51     返回分割後的字符串列表。
 52 ‘‘‘
 53 str = "Line1-abcdef \nLine2-abc \nLine4-abcd";
 54 print(str.split( )); #[‘Line1-abcdef‘, ‘Line2-abc‘, ‘Line4-abcd‘]
 55 print(str.split( , 1 ));#[‘Line1-abcdef‘, ‘\nLine2-abc \nLine4-abcd‘]
 56 
 57 ‘‘‘
 58  capitalize 把字符串的第一個字符大寫
 59 ‘‘‘
 60 print(mystr.capitalize()) # Hello world
 61 
 62 ‘‘‘
 63  title 把字符串的每個單詞首字母大寫
 64 ‘‘‘
 65 print(mystr.title()) # Hello World
 66 
 67 ‘‘‘
 68  startswith  檢查字符串是否是以 obj 開頭, 是則返回 True,否則返回 False
 69 ‘‘‘
 70 print(mystr.startswith(a)) #False
 71 
 72 ‘‘‘
 73  endswith 檢查字符串是否以obj結束,如果是返回True,否則返回 False.
 74 ‘‘‘
 75 print(mystr.endswith(d)) #True
 76 
 77 ‘‘‘
 78 lower 轉換 mystr 中所有大寫字符為小寫
 79 ‘‘‘
 80 print(mystr.lower()) #hello world
 81 
 82 ‘‘‘
 83 upper 轉換 mystr 中的小寫字母為大寫
 84 ‘‘‘
 85 print(mystr.upper()) #HELLO WORLD
 86 
 87 ‘‘‘
 88 ljust  返回一個原字符串左對齊,並使用空格填充至長度 width 的新字符串
 89 ‘‘‘
 90 print(mystr.ljust(20)) # hello world
 91 
 92 ‘‘‘
 93 rjust  返回一個原字符串右對齊,並使用空格填充至長度 width 的新字符串
 94 ‘‘‘
 95 print(mystr.rjust(20)) #          hello world
 96 
 97 ‘‘‘
 98 center  返回一個原字符串居中,並使用空格填充至長度 width 的新字符串
 99 ‘‘‘
100 print(mystr.center(20)) #    hello world
101 
102 ‘‘‘
103 rstrip 刪除 mystr 字符串末尾的空白字符
104 ‘‘‘
105 print(mystr.rstrip()) #hello world
106 
107 ‘‘‘
108 lstrip 刪除 mystr 左邊的空白字符
109 ‘‘‘
110 print(mystr.lstrip()) #hello world
111 
112 ‘‘‘
113 strip 刪除 mystr 字符串末尾的空白字符
114 ‘‘‘
115 print(mystr.strip()) #hello world

python基礎之字符串