python String(字符串)


'''什么是字符串
字符串是以單引號或雙引號括起來的任意文本
'abc'
"def"
字符串不可變
'''

#創建字符串
str1 = "sunck is a good man!"
str3 = "sunck is a nice man!"
str5 = "sunck is a handsome man!"

'''
字符串運算
'''
#字符串連接
str6 = "sunck is a "
str7 = "good man"
str8 = str6 + str7
print("str6 =", str6)
print("str7 =", str7)
print("str8 =", str8)
#輸出重復字符串
str9 = "good"
str10 = str9 * 3
print("str10 =", str10)
#訪問字符串中的某一個字符
#通過索引下標查找字符,索引從0開始
#字符串名[下標]
str11 = "sunck is a good man!凱"
print(str11[1])
#str11[1] = "a" #字符串不可變
print("str11 =", str11)

#截取字符串中的一部分
str13 = "sunck is a good man!"
#從給定下標出開始截取到給定下標之前
str15 = str13[6:15]
#從頭截取到給定下標之前
str16 = str13[:5]
#從給定下標處開始截取到結尾
str17 = str13[16:]
print("str17 =", str17)

str18 = "sunck is a good man"
print("good" in str18)
print("good1" not in str18)

#print(~5) #6
'''
00000101
11111010
10000110
'''

#格式化輸出
print("sunc is a good man")
num = 10
str19 = "sunck is a nice man!"
f = 10.1267
print("num =", num, "str19 =", str19)
# %d %s %f 占位符
# 精確到小數點后3位,會四舍五入
print("num = %d, str19 = %s, f = %.3f" % (num, str19, f))

'''
轉義字符 \
將一些字符轉換成有特殊含義的字符
'''
#\n
print("num = %d\nstr19 = %s\nf = %.3f" % (num, str19, f))
'''
\\
'''
print("sunck \\ is")

# \' \"
print('tom is a \'good\' man')
print("tom is a \"good\" man")
# tom is a 'good' man

#如果字符串內有很多換行,用\n寫在一行不好閱讀
print("good\nnice\nhandsome")
print('''
good
nice
handsome
''')

'''
\t 制表符
'''
print("sunck\tgood")


#如果字符中有好多字符串都需要轉義,就需要加入好多\,為了簡化,Python允許用r表示內部的字符串默認不轉義
# \\\t\\
print(r"\\\t\\")
print(r"C:\Users\xlg\Desktop\Python-1704\day03")
print("C:\\Users\\xlg\\Desktop\\Python-1704\\day03")
'''
windows
C:\\Users\\xlg\\Desktop\\Python-1704\\day03
linux
/root/user/sunck/Desktop/Python-1704/day03
'''


#eval(str)
#功能:將字符串str當成有效的表達式來求值並返回計算結果
num1 = eval("123")
print(num1)
print(type(num1))
print(eval("+123"))
print(eval("-123"))
print(eval("12+3"))
print(eval("12-3"))
#print(eval("a123"))
#print(eval("12a3"))


#len(str)
#返回字符串的長度(字符個數)
print(len("sunck is a good man凱"))

#lower()轉換字符串中大寫字母為小寫字母
str20 = "SUNCK is a Good Man!凱"
str21 = str20.lower()
print(str21)
print("str20 = %s" %(str20))

#upper()轉換字符串中小寫字母為大寫字母
str21 = "SUNCK is a Good Man!"
print(str21.upper())


#swapcase()轉換字符串中小寫字母為大寫字母,大寫字母為小寫字母
str22 = "SUNCK is a gOOd mAn!"
print(str22.swapcase())


#capitalize() 首字母大寫,其他小寫
str23 = "SUNCK is a gOOd mAn!"
print(str23.capitalize())


#title()每個單的首字母大寫
str24 = "SUNCK is a gOOd mAn!"
print(str24.title())

# character char
#center(width[, fillchar])
#返回一個指定寬度的居中字符串,fillchar為填充的字符串,默認空格填充
str25 = "kaige is a nice man"
print(str25.center(40,"*"))


#ljust(width[, fillchar])
#返回一個指定寬度的左對齊字符串,fllchar為填充字符,默認空格填充
str26 = "kaige is a nice man"
print(str26.ljust(40, "%"))



#rjust(width[, fillchar])
#返回一個指定寬度的右對齊字符串,fllchar為填充字符,默認空格填充
str27 = "kaige is a nice man"
print(str26.rjust(40, "%"))


#zfill(width)
#返回一個長度為width的字符串,原字符串右對齊,前面補0
str28 = "kaige is a nice man"
print(str28.zfill(40))

#count(str[,start][,end])
#返回字符串中strc出現的次數,可以指定一個范圍,默認從頭到尾
str29 = "kaige is a very very nice man"
print(str29.count("very",15, len(str29) ))


#find(str[, start][,end])
#從左向右檢測str字符串是否包含在字符串中,可以指定范圍,默認從頭到尾。得到的是第一次出現的開始下標,沒有返回-1
str30 = "kaige is a very very nice man"
print(str30.find("very"))
print(str30.find("good"))
print(str30.find("very", 15, len(str30)))


#rfind(str[, start][,end])]
str30 = "kaige is a very very nice man"
print(str30.rfind("very"))
#print(str30.rfind("good"))
print(str30.rfind("very", 0, 15))

#index(str, start=0, end=len(str))
#根find()一樣,只不過如果str不存在的時候回報一個異常
str31 = "kaige is a very very nice man"
#print(str31.index("good"))


#rindex(str, start=0, end=len(str))
#根rfind()一樣,只不過如果str不存在的時候回報一個異常
str32 = "kaige is a very very nice man"
print(str32.rindex("very"))


#lstrip()截掉字符串左側指定的字符,默認為空格
str33 = "*******kaige is a nice man"
print(str33.lstrip("*"))

#rstrip()截掉字符串右側指定的字符,默認為空格
str34 = "kaige is a nice man "
print(str34.rstrip(), "*")

str35 = "*******kaige is a nice man*********"
print(str35.strip("*"))


str36 = "a"
print(ord(str36))
str37 = chr(65)
print(str37)

print(" " != " ")


#split(str="", num)
#以str為分隔符截取字符串,指定num,則僅截取num個字符串
str38 = "sunck**is******a***good*man"
list39 = str38.split("*")
print(list39)
c = 0
for s in list39:
if len(s) > 0:
c += 1
print(c)



#splitlines([keepends]) 安裝('\r', '\r\n', '\n')分隔
#keepends == True 會保留換行符
str40 = '''sunck is a good man!
sunck is a nice man!
sunck is handsome man!
'''
print(str40.splitlines())



#join(seq) 以指定的字符串分隔符,將seq中的所有元素組合成一個字符串
list41 = ['sunck', 'is', 'a', 'good', 'man']
str42 = "&^%$#".join(list41)
print(str42)


#max() min()
str43 = "sunck is a good man!z"
print(max(str43))
print("*"+min(str43)+"*")


#replace(oldstr, newstr, count)
#用newstr替換oldstr,默認是全部替換。如果指定了count,那么只替換前count個
str44 = "sunck is a good good good man"
str45 = str44.replace("good", "nice", 1)
print(str44)
print(str45)


#創建一個字符串映射表
print("************")
# 要轉換的字符串 目標字符串
t46 = str.maketrans("ac", "65")
# a--6 c--5
str47 = "sunck is a good man"
str48 = str47.translate(t46)
print(str48)


#startswith(str, start=0, end=len(str)
#在給定的范圍內判斷是否是以給定的字符串開頭,如果沒有指定范圍,默認整個字符串
str49 = "sunck is a good man"
print(str49.startswith("sunck", 5, 16))

#endswith(str, start=0, end=len(str)
#在給定的范圍內判斷是否是以給定的字符串結尾,如果沒有指定范圍,默認整個字符串
str50 = "sunck is a nice man"
print(str50.endswith("man"))

#編碼
#encode(encoding="utf-8", errors="strict")
str51 = "sunck is a good man凱"
#ignore忽略錯誤
data52 = str51.encode("utf-8", "ignore")
print(data52)
print(type(data52))

#解碼 注意:要與編碼時的編碼格式一致
str53 = data52.decode("gbk", "ignore")
print(str53)

#isalpha()
#如果字符串中至少有一個字符且所有的字符都是字母返回True,否則返回False
str54 = "sunckisagoodman"
print(str54.isalpha())

#isalnum()
#如果字符串中至少有一個字符且所有的字符都是字母或數字返回True,否則返回False
str55 = "1a2b3"
print(str55.isalnum())


#isupper()
#如果字符串中至少有一個英文字符且所有的英文字符都是大寫的英文字母返回True,否則返回False
print("ABC".isupper())
print("1".isupper())
print("ABC1".isupper())
print("ABC#".isupper())


#islower()
#如果字符串中至少有一個英文字符且所有的英文字符都是小寫的英文字母返回True,否則返回False
print("abc".islower())
print("abcA".islower())
print("1".islower())
print("abc1".islower())
print("abc#".islower())


#istitle()
#如果字符串是標題化的返回True,否則返回False
print("Sunck Is".istitle())
print("Sunck is".istitle())
print("sunck is".istitle())

#isdigit()
#如果字符串中只包含數字字符返回True,否則范湖False
print("123".isdigit())
print("123a".isdigit())


#isnumeric()同上
print("123".isnumeric())
print("123a".isnumeric())


#字符串中只包含十進制字符
print("123".isdecimal())
print("123z".isdecimal())


#如果字符中只包含空格則返回True,否則返回False
print(" ".isspace())
print(" ".isspace())
print("\t".isspace())
print("\n".isspace())
print("\r".isspace())


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM