python字符串应用
1、字符串定義
就是引號里的內容?? 單引號、雙引號、三引號
2、原始字符串
(1)‘\’轉義
>>> print ("c:\now")
c:
ow
>>> print ("c:\\now")
c:\now
(2)字符串有很多反斜杠時需使用原始字符串,只需在字符串前面加r
>>> print (r'c:\now')
c:\now
3、字符串運算符
(1)+ 字符串拼接
>>> str1='i am'
>>> str2='a boy'
>>> str3=str1+str2
>>> str3
'i ama boy'
(2)* 字符串重復
>>> a="hello"
>>> a*2
'hellohello'
(3)[] 字符串截取
>>> a='hello world'
>>> a[1:-1:2]
'el ol'
(4)in/not in 判斷字符串是否包含給定的字符串
>>> a='hello world'
>>> 'id' in a
False
4、字符串內建函數
split()分割字符串
casefold()將整個字符串的所有字符改為小寫
center(width)將字符串居中。并使用空格填充至width的新字符串
ljust(width)返回一個左對齊的字符串,并使用空格填充至長度為width的新字符串
rjust(width)返回一個右對齊的字符串,并使用空格填充至長度為width的新字符串
count(sub,start,end)返回sub在字符串中出現的次數,start和end表示參數范圍
encode()以encode指定的編碼格式對字符串進行編碼
endwith(sub,start,end)檢查字符串是否以sub子字符串結束,返回true,否則返回false
expandtabs()把字符串的tab符號(\t)轉換為空格,如不指定參數默認空格數是tabsize=8
find(sub,start,end)檢查sub是否包含在字符串中,如果有則換回索引值,否則返回-1,start,end參數可選
isalnum()字符串至少有一個字符并且所有字符都是字母或者數字返回T入耳,否則返回fals
isalpha()都是字母返回True
islower()所有字符都是小寫返回True
isupper()所有字母
istitle()所有字母只有單詞首字母大寫其他的都是小寫
join(sub)以字符串作為分隔符,插入sub中
lower()轉換所有的大寫為小寫
upper()轉換所有的小寫為大寫
lstrip()去掉字符串中左邊的空格
partition(sub)找到子字符串sub把這個字符串隔成3個,組成一個元祖
replace(old,new,count)將字符串中old子字符串換成new的子字符串,如果count指定則替換,不超過count次數
zfill(width)返回長度為width的字符串,原字符串右對齊,前邊用0填充
capitalize()字符串首字母大寫
?
# 1 首字母大寫
?? ??? ??? ??? ??? ??? ?# test = "aLex"
?? ??? ??? ??? ??? ??? ?# v = test.capitalize()
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ?# 2 所有變小寫,casefold更牛逼,很多未知的對相應變小寫
?? ??? ??? ??? ??? ??? ?# v1 = test.casefold()
?? ??? ??? ??? ??? ??? ?# print(v1)
?? ??? ??? ??? ??? ??? ?# v2 = test.lower()
?? ??? ??? ??? ??? ??? ?# print(v2)
?? ??? ??? ??? ??? ?# 3 設置寬度,并將內容居中
?? ??? ??? ??? ??? ??? ?# 20 代指總長度
?? ??? ??? ??? ??? ??? ?# *? 空白未知填充,一個字符,可有可無
?? ??? ??? ??? ??? ??? ?# v = test.center(20,"中")
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ??? ?# test = "alex"
?? ??? ??? ??? ??? ??? ?# v = test.ljust(20,"*")
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ??? ?# test = "alex"
?? ??? ??? ??? ??? ??? ?# v = test.rjust(20,"*")
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ??? ?# test = "alex"
?? ??? ??? ??? ??? ??? ?# v = test.zfill(20)
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ?# 4 去字符串中尋找,尋找子序列的出現次數
?? ??? ??? ??? ??? ??? ?# test = "aLexalexr"
?? ??? ??? ??? ??? ??? ?# v = test.count('ex')
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ??? ?# test = "aLexalexr"
?? ??? ??? ??? ??? ??? ?# v = test.count('ex',5,6)
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ?# 欠
?? ??? ??? ??? ??? ?# encode
?? ??? ??? ??? ??? ?# decode
?? ??? ??? ??? ??? ?# 5
?? ??? ??? ??? ??? ??? ?# 以什么什么結尾
?? ??? ??? ??? ??? ??? ?# 以什么什么開始
?? ??? ??? ??? ??? ??? ?# test = "alex"
?? ??? ??? ??? ??? ??? ?# v = test.endswith('ex')
?? ??? ??? ??? ??? ??? ?# v = test.startswith('ex')
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ?# 6 expandtabs,斷句20,
?? ??? ??? ??? ??? ??? ?# test = "username\temail\tpassword\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123"
?? ??? ??? ??? ??? ??? ?# v = test.expandtabs(20)
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ?# 7 從開始往后找,找到第一個之后,獲取其未知
?? ??? ??? ??? ??? ??? ?# > 或 >=
?? ??? ??? ??? ??? ??? ?# test = "alexalex"
?? ??? ??? ??? ??? ??? ?# 未找到 -1
?? ??? ??? ??? ??? ??? ?# v = test.find('ex')
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ?# 8 index找不到,報錯?? 忽略
?? ??? ??? ??? ??? ??? ?# test = "alexalex"
?? ??? ??? ??? ??? ??? ?# v = test.index('8')
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ?# 9 格式化,將一個字符串中的占位符替換為指定的值
?? ??? ??? ??? ??? ??? ?# test = 'i am {name}, age {a}'
?? ??? ??? ??? ??? ??? ?# print(test)
?? ??? ??? ??? ??? ??? ?# v = test.format(name='alex',a=19)
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ??? ?# test = 'i am {0}, age {1}'
?? ??? ??? ??? ??? ??? ?# print(test)
?? ??? ??? ??? ??? ??? ?# v = test.format('alex',19)
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ?# 10 格式化,傳入的值 {"name": 'alex', "a": 19}
?? ??? ??? ??? ??? ??? ?# test = 'i am {name}, age {a}'
?? ??? ??? ??? ??? ??? ?# v1 = test.format(name='df',a=10)
?? ??? ??? ??? ??? ??? ?# v2 = test.format_map({"name": 'alex', "a": 19})
?? ??? ??? ??? ??? ?# 11 字符串中是否只包含 字母和數字
?? ??? ??? ??? ??? ??? ?# test = "123"
?? ??? ??? ??? ??? ??? ?# v = test.isalnum()
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ??? ?# str
?? ??? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?# 12 是否是字母,漢子
?? ??? ??? ??? ??? ??? ?# test = "as2df"
?? ??? ??? ??? ??? ??? ?# v = test.isalpha()
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ?# 13 當前輸入是否是數字
?? ??? ??? ??? ??? ??? ?# test = "二" # 1,②
?? ??? ??? ??? ??? ??? ?# v1 = test.isdecimal()
?? ??? ??? ??? ??? ??? ?# v2 = test.isdigit()
?? ??? ??? ??? ??? ??? ?# v3 = test.isnumeric()
?? ??? ??? ??? ??? ??? ?# print(v1,v2,v3)
?? ??? ??? ??? ??? ?# 14 是否存在不可顯示的字符
?? ??? ??? ??? ??? ??? ?# \t?? 制表符
?? ??? ??? ??? ??? ??? ?# \n?? 換行
?? ??? ??? ??? ??? ??? ?# test = "oiuas\tdfkj"
?? ??? ??? ??? ??? ??? ?# v = test.isprintable()
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ?# 15 判斷是否全部是空格
?? ??? ??? ??? ??? ??? ?# test = ""
?? ??? ??? ??? ??? ??? ?# v = test.isspace()
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ?# 16 判斷是否是標題
?? ??? ??? ??? ??? ??? ?# test = "Return True if all cased characters in S are uppercase and there is"
?? ??? ??? ??? ??? ??? ?# v1 = test.istitle()
?? ??? ??? ??? ??? ??? ?# print(v1)
?? ??? ??? ??? ??? ??? ?# v2 = test.title()
?? ??? ??? ??? ??? ??? ?# print(v2)
?? ??? ??? ??? ??? ??? ?# v3 = v2.istitle()
?? ??? ??? ??? ??? ??? ?# print(v3)
?? ??? ??? ??? ??? ?# 17 ***** 將字符串中的每一個元素按照指定分隔符進行拼接
?? ??? ??? ??? ??? ??? ?# test = "你是風兒我是沙"
?? ??? ??? ??? ??? ??? ?# print(test)
?? ??? ??? ??? ??? ??? ?# # t = ' '
?? ??? ??? ??? ??? ??? ?# v = "_".join(test)
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ?# 18 判斷是否全部是大小寫 和 轉換為大小寫
?? ??? ??? ??? ??? ??? ?# test = "Alex"
?? ??? ??? ??? ??? ??? ?# v1 = test.islower()
?? ??? ??? ??? ??? ??? ?# v2 = test.lower()
?? ??? ??? ??? ??? ??? ?# print(v1, v2)
?? ??? ??? ??? ??? ??? ?# v1 = test.isupper()
?? ??? ??? ??? ??? ??? ?# v2 = test.upper()
?? ??? ??? ??? ??? ??? ?# print(v1,v2)
?? ??? ??? ??? ??? ?# 19
?? ??? ??? ??? ??? ??? ?# 移除指定字符串
?? ??? ??? ??? ??? ??? ?# 有限最多匹配
?? ??? ??? ??? ??? ??? ?# test = "xa"
?? ??? ??? ??? ??? ??? ?# # v = test.lstrip('xa')
?? ??? ??? ??? ??? ??? ?# v = test.rstrip('9lexxexa')
?? ??? ??? ??? ??? ??? ?# # v = test.strip('xa')
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ??? ?# test.lstrip()
?? ??? ??? ??? ??? ??? ?# test.rstrip()
?? ??? ??? ??? ??? ??? ?# test.strip()
?? ??? ??? ??? ??? ??? ?# 去除左右空白
?? ??? ??? ??? ??? ??? ?# v = test.lstrip()
?? ??? ??? ??? ??? ??? ?# v = test.rstrip()
?? ??? ??? ??? ??? ??? ?# v = test.strip()
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ??? ?# print(test)
?? ??? ??? ??? ??? ??? ?# 去除\t \n
?? ??? ??? ??? ??? ??? ?# v = test.lstrip()
?? ??? ??? ??? ??? ??? ?# v = test.rstrip()
?? ??? ??? ??? ??? ??? ?# v = test.strip()
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ?# 20 對應關系替換
?? ??? ??? ??? ??? ??? ?# test =? "aeiou"
?? ??? ??? ??? ??? ??? ?# test1 = "12345"
?? ??? ??? ??? ??? ??? ?# v = "asidufkasd;fiuadkf;adfkjalsdjf"
?? ??? ??? ??? ??? ??? ?# m = str.maketrans("aeiou", "12345")
?? ??? ??? ??? ??? ??? ?# new_v = v.translate(m)
?? ??? ??? ??? ??? ??? ?# print(new_v)
?? ??? ??? ??? ??? ?# 21 分割為三部分
?? ??? ??? ??? ??? ??? ?# test = "testasdsddfg"
?? ??? ??? ??? ??? ??? ?# v = test.partition('s')
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ??? ?# v = test.rpartition('s')
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ?# 22 分割為指定個數
?? ??? ??? ??? ??? ??? ?# v = test.split('s',2)
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ??? ?# test.rsplit()
?? ??? ??? ??? ??? ?# 23 分割,只能根據,true,false:是否保留換行
?? ??? ??? ??? ??? ??? ?# test = "asdfadfasdf\nasdfasdf\nadfasdf"
?? ??? ??? ??? ??? ??? ?# v = test.splitlines(False)
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ?#? 24 以xxx開頭,以xx結尾
?? ??? ??? ??? ??? ??? ?# test = "backend 1.1.1.1"
?? ??? ??? ??? ??? ??? ?# v = test.startswith('a')
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ??? ?# test.endswith('a)
?? ??? ??? ??? ??? ?# 25 大小寫轉換
?? ??? ??? ??? ??? ??? ?# test = "aLex"
?? ??? ??? ??? ??? ??? ?# v = test.swapcase()
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ?# 26 字母,數字,下劃線 : 標識符 def? class
?? ??? ??? ??? ??? ??? ?# a = "def"
?? ??? ??? ??? ??? ??? ?# v = a.isidentifier()
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ?# 27 將指定字符串替換為指定字符串
?? ??? ??? ??? ??? ??? ?# test = "alexalexalex"
?? ??? ??? ??? ??? ??? ?# v = test.replace("ex",'bbb')
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ??? ?# v = test.replace("ex",'bbb',2)
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ?###################### 7個基本魔法 ######################
?? ??? ??? ??? ??? ?# join?????? # '_'.join("asdfasdf")
?? ??? ??? ??? ??? ?# split
?? ??? ??? ??? ??? ?# find
?? ??? ??? ??? ??? ?# strip
?? ??? ??? ??? ??? ?# upper
?? ??? ??? ??? ??? ?# lower
?? ??? ??? ??? ??? ?# replace
?? ??? ??? ??? ??? ?###################### 4個灰魔法 ######################
?? ??? ??? ??? ??? ?# test = "鄭建文妹子有種沖我來"
?? ??? ??? ??? ??? ?# 一、for循環
?? ??? ??? ??? ??? ??? ?# for 變量名 in 字符串:
?? ??? ??? ??? ??? ??? ?#???? 變量名
?? ??? ??? ??? ??? ??? ?# break
?? ??? ??? ??? ??? ??? ?# continue
?? ??? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ??? ?# index = 0
?? ??? ??? ??? ??? ??? ?# while index < len(test):
?? ??? ??? ??? ??? ??? ?#???? v = test[index]
?? ??? ??? ??? ??? ??? ?#???? print(v)
?? ??? ??? ??? ??? ??? ?#
?? ??? ??? ??? ??? ??? ?#???? index += 1
?? ??? ??? ??? ??? ??? ?# print('=======')
?? ??? ??? ??? ??? ??? ?# for zjw in test:
?? ??? ??? ??? ??? ??? ?#???? print(zjw)
?? ??? ??? ??? ??? ??? ?# test = "鄭建文妹子有種沖我來"
?? ??? ??? ??? ??? ??? ?# for item in test:
?? ??? ??? ??? ??? ??? ?#???? print(item)
?? ??? ??? ??? ??? ??? ?#???? break
?? ??? ??? ??? ??? ??? ?# for item in test:
?? ??? ??? ??? ??? ??? ?#???? continue
?? ??? ??? ??? ??? ??? ?#???? print(item)
?? ??? ??? ??? ??? ?# 二、索引,下標,獲取字符串中的某一個字符
?? ??? ??? ??? ??? ??? ?# v = test[3]
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ?# 三、切片
?? ??? ??? ??? ??? ??? ?# v = test[0:-1] # 0=<? <1
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ?# 四、獲取長度
?? ??? ??? ??? ??? ??? ?# Python3: len獲取當前字符串中由幾個字符組成
?? ??? ??? ??? ??? ??? ?# v = len(test)
?? ??? ??? ??? ??? ??? ?# print(v)
?? ??? ??? ??? ??? ?# 注意:
?? ??? ??? ??? ??? ?# len("asdf")
?? ??? ??? ??? ??? ?# for循環
?? ??? ??? ??? ??? ?# 索引
?? ??? ??? ??? ??? ?# 切片
?? ??? ??? ??? ??? ?# 五、獲取連續或不連續的數字,
?? ??? ??? ??? ??? ??? ?# Python2中直接創建在內容中
?? ??? ??? ??? ??? ??? ?# python3中只有for循環時,才一個一個創建
?? ??? ??? ??? ??? ??? ?# r1 = range(10)
?? ??? ??? ??? ??? ??? ?# r2 = range(1,10)
?? ??? ??? ??? ??? ??? ?# r3 = range(1,10,2)
?? ??? ??? ??? ??? ??? ?# 幫助創建連續的數字,通過設置步長來指定不連續
?? ??? ??? ??? ??? ??? ?# v = range(0, 100, 5)
?? ??? ??? ??? ??? ??? ?#
?? ??? ??? ??? ??? ??? ?# for item in v:
?? ??? ??? ??? ??? ??? ?#???? print(item)
?? ??? ??? ??? ??? ?##### 練習題:根據用戶輸入的值,輸出每一個字符以及當前字符所在的索引位置 #####
?? ??? ??? ??? ??? ??? ?# test = input(">>>")
?? ??? ??? ??? ??? ??? ?# for item in test:
?? ??? ??? ??? ??? ??? ?#???? print(item)
?? ??? ??? ??? ??? ??? ?# 將文字 對應的索引打印出來:
?? ??? ??? ??? ??? ??? ?# test = input(">>>")
?? ??? ??? ??? ??? ??? ?# print(test)?? # test = qwe?? test[0]?? test[1]
?? ??? ??? ??? ??? ??? ?# l = len(test) # l = 3
?? ??? ??? ??? ??? ??? ?# print(l)
?? ??? ??? ??? ??? ??? ?#
?? ??? ??? ??? ??? ??? ?# r = range(0,l) # 0,3
?? ??? ??? ??? ??? ??? ?# for item in r:
?? ??? ??? ??? ??? ??? ?#???? print(item, test[item]) # 0 q,1 w,2 e
?? ??? ??? ??? ??? ??? ?# test = input(">>>")
?? ??? ??? ??? ??? ??? ?# for item in range(0, len(test)):
?? ??? ??? ??? ??? ??? ?#???? print(item, test[item])
?? ??? ??? ??? ??? ?###################### 1個深灰魔法 ######################
?? ??? ??? ??? ??? ?# 字符串一旦創建,不可修改
?? ??? ??? ??? ??? ?# 一旦修改或者拼接,都會造成重新生成字符串
?? ??? ??? ??? ??? ?# name = "zhengjianwen"
?? ??? ??? ??? ??? ?# age = "18"
?? ??? ??? ??? ??? ?#
?? ??? ??? ??? ??? ?# info = name + age
?? ??? ??? ??? ??? ?# print(info)
?
轉載于:https://www.cnblogs.com/fat-girl-spring/p/9121777.html
總結
以上是生活随笔為你收集整理的python字符串应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CSS学习摘要-数值和单位及颜色
- 下一篇: 开发实施习惯