Python3.6 str內置方法

s = str("Hello Python World!!")

# Demo1 字符串拼接
s1_1 = "Hello Python World!!"
s1_2 = "Plus"
s1_3 = s1_1 + s1_2
print(s1_3)
# "Hello Python World!!Plus"
s1_3 = s1_1[0:5] + s1_2[3]
print(s1_3)
# "Hellos"
# 切片[0:5]包含第一個位置,不包含第二個位置

# Demo2 Capitalize 首字母大寫
s2_1 = "hello world!!"
s2_2 = s2_1.capitalize()
print(s2_2)
# "Hello world!!"

# Demo3 casefold 字符全部統一大小寫(這個不推薦用因爲貌似會根據不同系統,轉換成大寫或者小寫)
s3_1 = "HELLO PYTHON"
s3_2 = s3_1.casefold()
print(s3_2)
# "hello python"

# Demo4 center 第一個參數爲生成的字符串的長度,第二個參數爲填充的字符串
s4_1 = "Hello Python"
s4_2 = s4_1.center(20, '*')
print(s4_2)
# "****Hello Python****"

# Demo5 count 第一個參數是需要數的字符或者字符串,第二第三個參數是起始位置(包含)和結束位置(不包含)
# 返回值爲有多少個
s5_1 = "Hello Python"
print(s5_1.count('l'))
# 2
print(s5_1.count('l', 2, 4))
# 2
print(s5_1.count('l', 0, 3))
# 1

# Demo6 endswith 以什麼結尾
s6_1 = "Hello Python!!!"
print(s6_1.endswith("!!"))
# True
print(s6_1.endswith("!!", 0, 5))
# False

# Demo7 expandtabs 把製表符修改爲空格,數字爲幾個空格
s7_1 = "tab\ttab\t!"
print(s7_1)
# tab	tab	!
print(s7_1.expandtabs(15))
# tab            tab            !

# Demo8 find 返回第一個找到的字符位置,找不到返回-1
s8_1 = "Hello World!!"
print(s8_1.find('l', 3))
# 3

# Demo9 index 該方法與find類似,但是如果沒找到會報個錯
s9_1 = "Hello Python!!!"
print(s9_1.index('l'))
# 2
# print(s9_1.index("?")) 找不到報錯
# ValueError: substring not found

# Demo10 isalnum 檢查字符串是否全部爲數字字符或者字母字符
s10_1 = "1234"
s10_2 = "abcd123AD4"
s10_3 = "1!22"

print(s10_1.isalnum())
# True
print(s10_2.isalnum())
# True
print(s10_3.isalnum())
# False

# Demo11 isalpha 是否全部爲字母字符
s11_1 = "abcdAbcd"
s11_2 = "Abcd1234"
s11_3 = "@abcdAbcd"
print(s11_1.isalpha())
# True
print(s11_2.isalpha())
# False
print(s11_3.isalpha())
# False

# Demo12 isdecimal 是否全部爲數字字符
s12_1 = "123123"
s12_2 = "Abcd1234"
s12_3 = "@123123"
print(s12_1.isdecimal())
# True
print(s12_2.isdecimal())
# False
print(s12_3.isdecimal())
# False

# Demo13 isdigit 是否全部爲數字字符,判斷數字的有好幾個,這個是對字符匹配最全的基本除了大寫的一、二、這種其餘都行,推薦就用這個
s13_1 = "123123"
s13_2 = "Abcd1234"
s13_3 = "@123123"
print(s13_1.isdigit())
# True
print(s13_2.isdigit())
# False
print(s13_3.isdigit())
# False

# Demo14 isidentifier 判斷變量名是否合法
print("_a".isidentifier())
print("1A".isidentifier())
print("AAA".isidentifier())

# Demo15 islower 如果全部是小寫字符返回True(測試下來,基本上只要不是大寫字母都可以)
print("aavvdd".islower())
# True
print("Abbb".islower())
# False
print("{}@bbb!22我".islower())
# True

# Demo16 isspace  如果全部是空格,返回Ture
print("   ".isspace())
# True
print(" a a".isspace())
# True
print("a a ".isspace())
# True

# Demo17 istitle 首字母是否大寫
print("Abvd".istitle())
# True
print("BdbB".istitle())
# False
print("abcd".istitle())
# False
print("1A1b".istitle())
# False

# Demo18 isupper 是否全部爲大寫(不考慮符號)如果出現小寫,那就返回false
print("ABCD".isupper())
# Ture
print("Abcd".isupper())
# False
print("A@B!".isupper())
# True

# Demo19 join 這個蠻有意思的,在一個可迭代的容器內用給予的字符串分割
li = ["abcd", "AAA", "@@@", "bcda"]
print("我是分割線".join(li))
# abcd我是分割線AAA我是分割線@@@我是分割線bcda

# Demo20 ljust 左對齊
print("aaa".ljust(10, '*'))
# aaa*******

# Demo21 lower 把字符串全部改成小寫
print("ABCD".lower())
# abcd
print("A@A#".lower())
# a@a#
print("aabs\Scd".lower())
# aabs\scd

# Demo22 lstrip 刪除左邊的空格或者字符
print("    aaaa".lstrip())
# aaaa
print("****abcd".lstrip('*'))
# abcd
print("ABCDssss".lstrip("ABCD"))
# ssss

# Demo23 maketrans 字符替代,將所有的字符都轉換成所給字符
intab = "aeiou"
outab = "12345"

print("my wife is a pig hahaha".translate(str.maketrans(intab, outab)))
# my w3f2 3s 1 p3g h1h1h1
print("my wife is a pig hahaha".translate(str.maketrans({'a': '1', 'e': '2', 'i': '3', 'o': '4', 'u': '5'})))
# my w3f2 3s 1 p3g h1h1h1

# Demo24 partition 將字符串分割成三份,參數字符串爲中間一份
print("ABCD1234abcd".partition("1"))
# ('ABCD', '1', '234abcd') 返回值是一個tuple

# Demo25 replace
print("aaaaa".replace('a', '1'))
# 11111
print("aaaaa".replace('a', '1', 2))
# 11aaa
print("aaaaa".replace("aa", "22", 2))
# 2222a

# Demo26 rfind 右起查找字符串或者字符
print("aaaaa".rfind('a'))
# 4

# Demo27 rindex 右起查找字符串或者字符,如果找不到會報錯
print("aAaAaAa".rindex('a'))
# 6
try:
    print("aAaAaAa".rindex('b'))
except Exception as e:
    print(e)
# ValueError: substring not found

# Demo28 rjust 類似ljust
print("aaaa".rjust(10, '*'))
# ******aaaa

# Demo29 rpartition partition是從左開始找到指定字符串,然後分三段,rpartition是從右開始找
print("aabbccdd112211AABBCCDD".rpartition("11"))
# ('aabbccdd1122', '11', 'AABBCCDD')

# Demo30 rsplit 第一個參數分隔符,默認爲所有的空字符,包括空格、換行(\n)
# 第二個參數是分割次數,分割兩次可是會變成三份的哦
# 他的分割是從右開始數,split是從左開始
print("aa bb cc dd".rsplit())
# ['aa', 'bb', 'cc', 'dd']
print("aa bb cc dd".rsplit(maxsplit=2))
# ['aa bb', 'cc', 'dd']

# Demo31 rstrip 去除右起字符,默認是空格
print("***aaa***".rstrip('*'))
# ***aaa

# Demo32 split 通過指定分隔符對字符串進行切片,如果參數num 有指定值,則僅分隔num次(1次是2個)
l1 = "a b c d 1 234".split()
print(type(l1), l1)
l2 = "a,b,c,d,e,123".split(',', 4)
print(type(l2), l2)
# <class 'list'> ['a', 'b', 'c', 'd', '1', '234']
# <class 'list'> ['a', 'b', 'c', 'd', 'e,123']

# Demo33 splitlines 分行,第二個參數爲是否保留換行符
s33_1 = "abcd1234\n1234abcd\ncccccc\n"
print(s33_1.splitlines())
print(s33_1.splitlines(keepends=True))
# ['abcd1234', '1234abcd', 'cccccc']
# ['abcd1234\n', '1234abcd\n', 'cccccc\n']

# Demo34 startswith 起始位置字符,返回True、False
print("abcd222".startswith('a', 0, 3))
# True
print("abcd222".startswith('bbb'))
# False

# Demo35 strip 去除首尾字符,默認爲空格
print("   aaa   ".strip())
# aaa
print("***aaa***".strip('*'))
# aaa

# Demo36 swapcase 切換大小寫
print("AAbbCCdd".swapcase())
# aaBBccDD

# Demo37 title 首字母大寫,剩下的全部小寫
print("aabbCCDD".title())
# Aabbccdd

# Demo38 translate 這個是和maketrans一起用的  也可以單獨使用只不過比較二
# 他接受一個字典,並且key,必須是int,那如果要替換'a'就需要用ascii碼,我下面就是用的ascii,97
# 而且python2 和 python3是有明顯區別的,要用就和maketrans一起用
print("ababab1212".translate({97: "F"}))
# FbFbFb1212

# Demo39 upper 全部轉換大寫
print("aabBccdD".upper())
# AABBCCDD

# Demo40 zfill 返回指定長度的字符串,原字符串右對齊,前面填充0
print("aaa".zfill(10))
# 0000000aaa

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