python 字符串經常使用操做

字符串經常使用方法 python

capitalize() git

String.capitalize() 將字符串首字母變爲大寫 api

  1. name = 'xiaoming'
  2.  
  3. new_name = name.capitalize()
  4.  
  5. print(new_name)

   

運行結果:
Xiaoming
數組

 

count() ide

String.count() 統計字符出現的次數 this

  1. name = 'xiaoming'
  2.  
  3. name_num = name.count('i')
  4.  
  5. print(name_num) # 2

   

center() 編碼

String.center() spa

#打印輸出字符,讓字符串放在中間 code

  1. name = 'Libai'
  2. print(name.center(50,'*'))

   

輸出結果以下: orm

**********************Libai***********************

 

endswith()

String.endswith() 判斷是否以指定的字符串結尾

  1. name = 'Libai'
  2.  
  3. new_val = name.endswith('bai')
  4. print(new_val)

結果爲:
True

 

find()

String.find() 查找字符串在原字符串中的位置,返回所在索引值

  1. name = 'this is test plaintext'
  2.  
  3. print(name.find('this'))
  4. print(name.find('is'))

   

在find()方法中,一樣可使用切片。

  1. name = 'this is test plaintext'
  2.  
  3. test_val = name[name.find('test'):12]
  4.  
  5. print(test_val) #test

   

字符串的切片用法與列表的使用方式一致。

 

format()

  1. String.format() 輸出指定的內容
  2. user_show_name = 'hello,{name},welcome to here,do you like ,{name}'
  3.  
  4. print(user_show_name.format(name='yanyan'))

   

輸出效果以下:

hello,yanyan,welcome to here,do you like ,yanyan

 

 

format_map()

  1. String.format_map() 將字典中的參數傳遞進字符串中,輸出
  2. hello = "My name is {name},I am {age} years old.I like {hobby}"
  3.  
  4. # 使用format_map()方法來傳遞值
  5. print(hello.format_map({'name':'yanyan','age':19,'hobby':'music travel'}))

   

isalnum()

String.isalnum() 判斷字符串中是否所有爲數字或者英文

  1. test_str01 = 'helloIam19yearsold'
  2. test_str02 = 'hello,I am 19 years old'

 

 

print(test_str01.isalnum()) # True

print(test_str02.isalnum()) # False

   

isalnum()方法判斷字符串中是否所有爲數字或者英文,符合就返回True,不符合就返回False,若是裏面包含有符號或者空格之類的特殊字符也會返回False。

 

isalpha()

String.isalpha() 判斷字符串中是否所有爲純英文字符

  1. test_str03 = 'hello I love you'
  2. test_str04 = 'helloILoveYou'

 

print(test_str03.isalpha()) # False

print(test_str04.isalpha()) # True

   

isdigit()

String.isdigit() 判斷字符串中是否所有爲整數

# isdigit() 判斷是否爲整數

  1. print('123'.isdigit()) # True
  2. print('hello'.isdigit()) # False

   

isidentifier()

String.isidentifier() 判斷是否是一個合法的標識符

# isidentifier() 判斷是否是一個合法的標識符

  1. print('test'.isidentifier()) # True
  2. print('12'.isidentifier()) # False
  3. print('_aa'.isidentifier()) # True

   

判斷字符串是否所有爲大寫或者小寫

islower

# islower() 判斷字符串是否所有是小寫

  1. print('Hello,world'.islower()) # False

 

 

isupper

# isupper() 判斷字符串是否所有爲大寫

  1. print('Hello,world'.isupper()) # False

   

join()

sep.join(seq) 鏈接字符串數組。將字符串、元組、列表中的元素以指定的字符(分隔符)鏈接生成一個新的字符串

  1. # 建立一個列表
  2. name = ['張學友','劉德華','郭富城','黎明']
  3.  
  4. print('--'.join(name))

   

輸出結果以下:

張學友--劉德華--郭富城--黎明

 

ljust()

String.ljust(size,替換符號) 從前向後開始計算,當字符串的長度超過size時,超過部分用替換符號替代

 

rjust()

String.rjust(size,替換符號) 從後向前開始計算,當字符串的長度超過size時,超過部分用替換符號替代

 

lower 將字符串大寫變成小寫

String.lower()

  1. # 建立一個字符串
  2. str = "hello,I am LiBai,I am 23 years old ,I like travel"
  3.  
  4. # lower 將字符串大寫變成小寫
  5. print(str.lower())

   

upper 將字符串小寫變成大寫

String.upper()

  1. # 建立一個字符串
  2. str = "hello,I am LiBai,I am 23 years old ,I like travel"
  3.  
  4. # 將字符串小寫變成大寫
  5. print(str.upper())

   

Tip:上面的lower()方法和upper()方法改變字符串後將改變的結果返回,可是本來的字符串並不會改變。

 

lstrip 去掉字符串左邊的空格或者回車

String.lstrip()

  1. print('-----------')
  2. # 建立一個字符串
  3. str = "\nhello,I am LiBai,I am 23 years old ,I like travel"
  4.  
  5. print(str.lstrip())

   

輸出結果以下:

-----------

hello,I am LiBai,I am 23 years old ,I like travel

除了lstrip 還有rstrip和 strip方法。

 

replace 替換

String.replace(old,new,count) 將字符串中的old字符替換爲New字符,count爲替換的個數

  1. str = 'hello,world,hello'
  2.  
  3. print(str.replace('hello','Hello',1))

   

輸出的效果以下:
Hello,world,hello

split

String.split() 切割

 

str = 'hello,world,hello'

 

  1. # 默認以空格爲分割
  2. print(str.split()) # ['hello,world,hello'] 單詞之間沒有空格,因此全部的內容爲一個元素
  3. # 以o爲分割
  4. print(str.split('o')) # ['hell', ',w', 'rld,hell', '']
  5. # 以逗號分割
  6. print(str.split(',')) # ['hello', 'world', 'hello']

 

   

splitlines() 以換行爲分割

String.splitlines()

  1. str = 'hello,\nworld,\nhello'
  2.  
  3. print(str.splitlines()) # ['hello,', 'world,', 'hello']

   

   

   

Tip:補充,python中的字符串並不容許修改值,只容許覆蓋值。

狀況以下:

  1. # 建立字符串
  2. str = 'hello,world'
  3. print(str[0]) # h
  4.  
  5. # 嘗試去修改
  6. str[0] = 'H'
  7. print(str) # TypeError: 'str' object does not support item assignment
  8.  
  9.  
  10. # 下面這種狀況是咱們常見的狀況,實際上是屬於一種字符串以前的值被新的值覆蓋掉了
  11. str = 'Hello,YanYan'
  12. print(str) # Hello,YanYan

 

 

 

 

 

 

 

 

 

 

 

 

總結

  1. name = " Ian is very good at\t {st} learning and eager to learn {sm2}."
  2.  
  3. print(name.capitalize()) #本來大寫改爲小寫,本來小寫改爲大寫
  4.  
  5. print(name.count("a")) #查找裏面字符"a"的數量
  6.  
  7. print(name.center(100,"-")) #第一個數字爲總共打印多少字符,不夠用-補充
  8.  
  9. print(name.ljust(100,'*')) #左邊的填充符號
  10.  
  11. print(name.rjust(100,'*')) #右邊的填充符號
  12.  
  13. print(name.expandtabs(tabsize=20)) #把tab符號的長度限定爲20字節
  14.  
  15. print(name.endswith("rn")) #這個是一個判斷輸出是,True and False
  16.  
  17. print(name.find("is")) #查找是第幾個字符開始
  18.  
  19. print(name[name.find("is"):]) #這個就能夠切片了
  20.  
  21. print(name.format(st = 'learning',sm2 = 'organization')) #就是之前學過的添加
  22.  
  23. #name.encode() #將字符串編碼成bytes格式
  24.  
  25. print('asd213'.isalpha()) #判斷是否爲純英文字符
  26.  
  27. print('asd213'.isalnum()) #判斷是否爲英文字母以及0-9
  28.  
  29. print('asd2221'.isdigit()) #重點,判斷是否爲整數
  30.  
  31. print('Aa1'.isidentifier()) #判斷是不是合法的標識符,知足Python命名的變量名規則
  32.  
  33. print('sdf'.islower()) #判斷是不是小寫
  34.  
  35. print('ASA'.isupper()) #判斷是不是全是大寫
  36.  
  37. print('Asdf'.isnumeric()) #判斷是否只包含數字
  38.  
  39. print('Asdf'.istitle()) #判斷是否每一個首字母大寫
  40.  
  41. print('| '.join(['daf','fdaf','dfa'])) #很常見,將前面的字符加到後面的列表的中間
  42.  
  43. print('ALSX'.lower()) #把大寫變成小寫
  44.  
  45. print('ALads'.upper()) #把字符全是變成大寫
  46.  
  47. print('Asfdf\n \n'.strip()) #去掉兩邊的空格和回車
  48.  
  49. print('\nAsfdf\n'.lstrip()) #去掉左邊的空格和回車
  50.  
  51. print('Asfdf\n'.rstrip()) #去掉右邊的空格和回車
  52. print('\nAsfdf\n'.lstrip()) #去掉左邊的空格和回車
  53.  
  54. print('Asfdf\n'.rstrip()) #去掉右邊的空格和回車
  55.  
  56. print('dfadda'.replace('a','D',1)) #替換,前面的被後面的替換,若是隻想換一個就加一個數字
  57.  
  58. print('ffadsfaadfaaaa'.rfind('f')) #找到最後一個f,並返回是第幾位
  59.  
  60. print('dafd fa df'.split(' ')) #利用空格進行分割,變成列表
  61.  
  62. print('VIC YI'.swapcase()) #大寫變成小寫
  63.  
  64. print('afdfa fad'.title()) #把每一個單詞首字母大寫
相關文章
相關標籤/搜索