python 字符串

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

#三引號可以用來多行註釋

str1 = 'a'
str2 = "b"
str3 = '''b
cd
deff'''
print(str1)  #a
print(str2)  #b
print(str3)
#b
#cd
#deff

str3 = str1 + str2
str4 = str2*3 #bbb

str1 = 'hello hello wold' \
       'ni' \
    'hao'
print(str1)  #hello hello woldnihao   一行寫不下可以用該方法

#字符通過切片拼接
str1 = str1[:3] + str1[2: 5] +str1[10:]
print(str1)  #hellloo woldnihao

#字符串比較大小
str1 = [1, 2, 3]
str2 = [4, 5, 'a']
print(str1 > str2)  #False

str1 = 'c:\new\table'
print(str1)
# c:
#ew    able

str1 = 'c:\\new\\table'
print(str1)
# c:\new\table

#字符串前加r以爲這這段字符串爲原始字符串,即保留原始本意,不做各種轉意處理
str1 = r'c:\new\table'
print(str1)
# c:\new\table

#str2 = r'c:\new\table\'  #注意原始字符串尾部不能加\,否則會將報錯,因爲提前將引號轉意了
#上面這個可以寫成
str2 = r'c:\new\table' + '\\'
print(str2)  #c:\new\table\

#格式化format
str4 = 'hello {1} ni men {0} hao ma {2}'.format('a', 'b', 'c')
print(str4)  #hello b ni men a hao ma c
str4 = 'hello {a} ni men {c} hao ma {d}'.format(c = '1', a = '2', d = '3')
print(str4)  #hello 2 ni men 1 hao ma 3
str4 = 'hello {0} ni men {c} hao ma {d}'.format('1', d = '2', c = '3')
print(str4)  #hello 1 ni men 3 hao ma 2
#str4 = 'hello {a} ni men {c} hao ma {0}'.format(a = '1', c = '2', '3') #混用的時候數值的形式不能放在索引的後面,否則出錯

str5 = 'hello %s ni men %d hao ma %f' % ('a', 11, 3.4)
print(str5)  #hello a ni men 11 hao ma 3.400000

#返回一個首字母大寫的字符串capitalize()
str1 = 'hello world'
str2 = str1.capitalize()
print(str2)  #Hello world

#設置字符串長度, 設置居中center, 參數1寬度, 參數2是不足區域的補充字符,默認空
str3 = str1.center(50, '-')
print(str3) #-------------------hello world--------------------

#設置字符串長度,左對齊,長度不足用設置的字符填充,默認空字符
str1 = 'hello'
str2 = str1.ljust(10, '-')
print(str2) #hello-----

#設置字符串長度, 右對齊,長度不足用設置的字符填充,默認空字符
str1 = 'hello'
str2 = str1.rjust(10, '-')
print(str2) #-----hello

#設置字符串長度, 長度不足用0填充
str1 = 'hello'
str2 = str1.zfill(10)
print(str2) #00000hello

#查找某個子字符串出現的數量count
print(str1.count('ll'))  #1
print(str1.count('l', 0, 3)) #1   參數2爲起始位置,  參數3爲終止位置, 區間[0,3)

#是否以某字符串開頭/結尾startswith/endswith
print(str1.startswith('he', 0, len(str1)-1)) #true
print(str1.endswith('rl', 0, len(str1)-1)) #true

#把字符串中的TAB換成空格,默認是一個tab等於8個空格
str1 = '    hello,      world'
str1 = str1.expandtabs(4)
print(str1) #    hello,      world

#查找子字符串並返回第一次找到的索引
str1 = 'hello,llo world!'
print(str1.find('ll')) #2
print(str1.find('ll', 3)) #6
print(str1.find('ll', 0, 6)) #2
print(str1.find('lll',)) #-1

#查找子字符串從右往左查並返回第一次找到的索引
str1 = 'hello,llo world!'
print(str1.rfind('ll')) #6
print(str1.rfind('ll', 3)) #6
print(str1.rfind('ll', 0, 6)) #2
print(str1.rfind('lll',)) #-1

#查找子字符串的索引,同上,找不到時有差異
print(str1.rindex('ll')) #6
print(str1.rindex('ll', 3)) #6
print(str1.rindex('ll', 0, 6)) #2
#print(str1.index('lll',)) #ValueError: substring not found

#查找子字符串的索引,同上,找不到時有差異
print(str1.index('ll')) #2
print(str1.index('ll', 3)) #6
print(str1.index('ll', 0, 6)) #2
#print(str1.index('lll',)) #ValueError: substring not found

#查找在不在字符串中
print('llo' in str1)  #True
print('llog' not in str1)  #True

#是否全是字母和數字,並至少有一個字符
str1 = 'hello world'
str2 = 'hello world1'
str3 = '1231244'
str4 = 'helloworld'
str5 = 'hello1world2'
str6 = ''
print(str1.isalnum())  #False
print(str2.isalnum())  #False
print(str3.isalnum())  #True
print(str4.isalnum())  #True
print(str5.isalnum())  #True
print(str6.isalnum())  #False

#是否全是字母並至少有1個字符
str1 = 'hello world'
str2 = 'hello world1'
str3 = '1231244'
str4 = 'helloworld'
str5 = 'hello1world2'
str6 = ''
print(str1.isalpha())  #False
print(str2.isalpha())  #False
print(str3.isalpha())  #False
print(str4.isalpha())  #True
print(str5.isalpha())  #False
print(str6.isalpha())  #False

#是否全是字母並至少有1個字符
str1 = 'hello world'
str2 = 'hello world1'
str3 = '1231244'
str4 = 'helloworld'
str5 = 'hello1world2'
str6 = ''
print(str1.isdigit())  #False
print(str2.isdigit())  #False
print(str3.isdigit())  #True
print(str4.isdigit())  #False
print(str5.isdigit())  #False
print(str6.isdigit())  #False

#是否全是空白符(空格/回車/換行/TAB),並至少有1個字符
str1 = 'hello world'
str2 = '    '#4個空格
str3 = '    '#1個tab
str4 = ''
str5 = '\n\r'
print(str1.isspace())  #False
print(str2.isspace())  #True
print(str3.isspace())  #True
print(str4.isspace())  #False
print(str5.isspace())  #True 回車換行符也算空白符

#是否全是小寫字母
str1 = 'hello world'
str2 = 'HELLO World1'
str3 = '1231244'
str4 = '@#$%^&'
str5 = '@#$%^&hello'
str6 = ''
print(str1.islower())  #True
print(str2.islower())  #False
print(str3.islower())  #False
print(str4.islower())  #False
print(str5.islower())  #True
print(str6.islower())  #False

#是否全是大寫字母
str1 = 'hello world'
str2 = 'HELLO WORLD'
str3 = '1231244'
str4 = '@#$%^&HELLO'
str5 = '@#$%^&'
str6 = ''
print(str1.isupper())  #False
print(str2.isupper())  #True
print(str3.isupper())  #False
print(str4.isupper())  #True
print(str5.isupper())  #False
print(str6.isupper())  #False

#是否全標題樣式(每個單詞首字母大寫)
str1 = 'Hello World'
str2 = 'Hello world'
str3 = '1231244'
str4 = '@#$%^&HELLO'
str5 = '@#$%^&'
str6 = ''
print(str1.istitle())  #True
print(str2.istitle())  #False
print(str3.istitle())  #False
print(str4.istitle())  #False
print(str5.istitle())  #False
print(str6.istitle())  #False

#S.join(seq) #把seq字符串序列,用S連接起來
str1 = '123'
str2 = str1.join('abcd')
print(str2)  #a123b123c123d

#轉化爲小寫
str1 = 'HeLLo WoRld'
str2 = str1.lower()
print(str2)  #hello world

#轉化爲大寫
str1 = 'HeLLo WoRld'
str2 = str1.upper()
print(str2)  #HELLO WORLD

#轉化爲標題樣式
str1 = 'HeLLo WoRld'
str2 = str1.title()
print(str2)  #Hello World

#大寫變小寫, 小寫變大寫
str1 = 'HeLLo WoRld'
str2 = str1.swapcase()
print(str2)  #hEllO wOrLD

#刪除左側空白字符
str1 = ' \n \r   HeLLo WoRld' #左側一個空格,一個TAB
str2 = str1.lstrip()
print(str2)  #HeLLo WoRld

#刪除右側空白字符
str1 = 'HeLLo WoRld \n \r   ' #左側一個空格,一個TAB
str2 = str1.rstrip()
print(str2)  #HeLLo WoRld

#刪除兩側空白字符
str1 = '\n HeLLo WoRld \n \r   ' #左側一個\n一個空格,右側一個空格,\n空格\r一個TAB
str2 = str1.strip()
print(str2)  #HeLLo WoRld

#S.partition(seq) 從S中找seq, 從左往右找到第一個後,咋把seq前面的部分,seq, 和seq後面的部分三個組成一個元組返回
str1 = 'hello world'
tuple1 = str1.partition('or')
print(tuple1)  #('hello w', 'or', 'ld')
tuple2 = str1.partition('l')
print(tuple2)  #('he', 'l', 'lo world')

#S.partition(seq) 從S中找seq, 從右往左找到第一個後,咋把seq前面的部分,seq, 和seq後面的部分三個組成一個元組返回
str1 = 'hello world'
tuple1 = str1.rpartition('or')
print(tuple1)  #('hello w', 'or', 'ld')
tuple2 = str1.rpartition('l')
print(tuple2)  #('hello wor', 'l', 'd')

#替換字符串中的子字符串
str1 = 'hello world lol'
str2 = str1.replace('o', 'm')
print(str2)  #hellm wmrld lml
str3 = str1.replace('o', 'm', 2) #第三個參數爲替換不超過多少次
print(str3)  #hellm wmrld lol

#S.split(seq, num) 把字符串S以seq從左到右進行切片,不超過num個
str1 = 'helloworldlol'
list1 = str1.split('o')
print(list1)  #['hell', 'w', 'rldl', 'l']
list2 = str1.split('o', 2)
print(list2)  #['hell', 'w', 'rldlol']

#S.split(seq, num) 把字符串S以seq從右到左進行切片,不超過num個
str1 = 'helloworldlol'
list1 = str1.rsplit('o')
print(list1)  #['hell', 'w', 'rldl', 'l']
list2 = str1.rsplit('o', 2)
print(list2)  #['hellow', 'rldl', 'l']

#S.splitlines() 把字符串S行的形式進行切片,相當於去除尾部1個\n,然後根據\n進行切片
str1 = 'hello\nworld\nlol'
list1 = str1.splitlines()
print(list1)  #['hello', 'world', 'lol']
str2 = ' \nhello\nworld\nlol\n '
list2 = str2.splitlines()
print(list2)  #[' ', 'hello', 'world', 'lol', ' ']
str3 = '\n\n\nhello\nworld\nlol\n\n\n'
list3 = str3.splitlines()
print(list3)  #['', '', '', 'hello', 'world', 'lol', '', '']

#相當於根據映射關係maketrans將字符串中的相關字符替換掉
str1 = 'eod'
str2 = '123' #str2長度必須和str1相同,不然maketrans報錯 ValueError: maketrans arguments must have same length
from string import maketrans
str3 = 'hello world lol'
str4 = str3.translate(maketrans(str1, str2))
print(str4)  #h1ll2 w2rl3 l2l
str5 = str3.translate(maketrans(str1, str2), 'hr') #刪除h和r字符
print(str5)  #1ll2 w2l3 l2l



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