天天看點

python字元串基礎,超級詳細,超全面

"""
字元串串是 Python 中最常⽤用的資料類型。我們⼀一般使⽤用引号來建立字元串串。建立字元串串很簡單,隻要為
變量量配置設定⼀一個值即可。
注意:三引号形式的字元串串⽀支援換⾏行行
"""
# name = input("請輸入一個名字:")
# print(type(name))

# 下标取字元串從0開始
s = "abcdefg"
print(s[0])
print(s[1])
print(s[2])

name = "abcdefgh"
print(name[2:5:1])  # cde
print(name[2:5])  # cde
print(name[:5])  # abcde
print(name[1:])  # bcdefg
print(name[:])  # abcdefg
print(name[::2])  # aceg 從開始到結束取2的步長02468 和range(0,10,2)一樣
print(name[:-1])  # abcdef, 負1表示倒數第⼀一個資料
print(name[-4:-1])  # def
print(name[::-1])  # gfedcba

"""
find():檢測某個⼦子串串是否包含在這個字元串串中,如果在傳回這個⼦子串串開始的位置下标,否則則傳回-1
字元串串序列列.find(⼦子串串, 開始位置下标, 結束位置下标)
"""
mystr = "hello world and itcast and itheima and Python"
print(mystr.find('and'))  # 12
print(mystr.find('and', 15, 30))  # 23
print(mystr.find('ands'))

"""
index():檢測某個⼦子串串是否包含在這個字元串串中,如果在傳回這個⼦子串串開始的位置下标,否則則
報異常。
字元串串序列列.index(⼦子串串, 開始位置下标, 結束位置下标)
"""
mystr = "hello world and itcast and itheima and Python"
print(mystr.index('and'))  # 12
print(mystr.index('and', 15, 30))  # 23
# print(mystr.index('ands'))  # 報錯


"""
rfind(): 和find()功能相同,但查找⽅方向為右側開始。
rindex():和index()功能相同,但查找⽅方向為右側開始。
count():傳回某個⼦子串串在字元串串中出現的次數
字元串串序列列.count(⼦子串串, 開始位置下标, 結束位置下标)
"""
mystr = "hello world and itcast and itheima and Python"
print(mystr.count('and'))  # 3
print(mystr.count('ands'))  # 0
print(mystr.count('and', 0, 20))  # 1

"""
replace():替換
字元串串序列列.replace(舊⼦子串串, 新⼦子串串, 替換次數)
注意:資料按照是否能直接修改分為可變類型和不不可變類型兩種。字元串串類型的資料修改的時候
不不能改變原有字元串串,屬于不不能直接修改資料的類型即是不不可變類型
"""
mystr = "hello world and itcast and itheima and Python"
# 結果: hello world he itcast he itheima he Python
print(mystr.replace('and', 'he'))
# 結果: hello world he itcast he itheima he Python
print(mystr.replace('and', 'he', 10))
# 結果: hello world and itcast and itheima and Python
print(mystr)


"""
split():按照指定字元分割字元串串。
字元串串序列列.split(分割字元, num)
注意: num表示的是分割字元出現的次數,即将來傳回資料個數為num+1個
"""
mystr = "hello world and itcast and itheima and Python"
# 結果: ['hello world ', ' itcast ', ' itheima ', ' Python']
print(mystr.split('and'))
# 結果: ['hello world ', ' itcast ', ' itheima and Python']
print(mystr.split('and', 2))
# 結果: ['hello', 'world', 'and', 'itcast', 'and', 'itheima', 'and', 'Python']
print(mystr.split(' '))
# 結果: ['hello', 'world', 'and itcast and itheima and Python']
print(mystr.split(' ', 2))
print(mystr)

"""
join():⽤用⼀一個字元或⼦子串串合并字元串串,即是将多個字元串串合并為⼀一個新的字元串串。
字元或⼦子串串.join(多字元串串組成的序列列)
"""
list1 = ['chuan', 'zhi', 'bo', 'ke']
t1 = ('aa', 'b', 'cc', 'ddd')
# 結果: chuan_zhi_bo_ke
print('_'.join(list1))
# 結果: aa...b...cc...ddd
print('...'.join(t1))

"""
capitalize():将字元串串第⼀一個字元轉換成⼤大寫
"""
mystr = "hello world and itcast and itheima and Python"
# 結果: Hello world and itcast and itheima and python
print(mystr.capitalize())

"""
title():将字元串串每個單詞⾸首字⺟母轉換成⼤大寫
"""
mystr = "hello world and itcast and itheima and Python"
# 結果: Hello World And Itcast And Itheima And Python
print(mystr.title())

"""
lower():将字元串串中⼤大寫轉⼩小寫。
"""
mystr = "Hello world and itcast and itheima and Python"
# 結果: hello world and itcast and itheima and python
print(mystr.lower())

"""
upper():将字元串串中⼩小寫轉⼤大寫。

"""
mystr = "hello world and itcast and itheima and Python"
# 結果: HELLO WORLD AND ITCAST AND ITHEIMA AND PYTHON
print(mystr.upper())

"""
lstrip():删除字元串串左側空⽩白字元
"""
mystr = "   hello world and itcast and itheima and Python    "
print(mystr.lstrip())  # 在終端才能看到效果

"""
rstrip():删除字元串串右側空⽩白字元。
"""
mystr = "   hello world and itcast and itheima and Python    "
print(mystr.rstrip())  # 在終端才能看到效果

"""
strip():删除字元串串兩側空⽩白字元。
"""

mystr = "   hello world and itcast and itheima and Python    "
print(mystr.strip())  # 在終端才能看到效果

"""
ljust():傳回⼀一個原字元串串左對⻬齊,并使⽤用指定字元(預設空格)填充⾄至對應⻓長度 的新字元串串。
"""
mystr = "hello"
print(mystr.ljust(10, "+"))

"""
rjust():傳回⼀一個原字元串串右對⻬齊,并使⽤用指定字元(預設空格)填充⾄至對應⻓長度 的新字元串串,文法和
ljust()相同。
center():傳回⼀一個原字元串串居中對⻬齊,并使⽤用指定字元(預設空格)填充⾄至對應⻓長度 的新字元串串,語
法和ljust()相同
"""


"""
startswith():檢查字元串串是否是以指定⼦子串串開頭,是則傳回 True,否則傳回 False。如果設定開
始和結束位置下标,則在指定範圍内檢查
字元串串序列列.startswith(⼦子串串, 開始位置下标, 結束位置下标)
"""
mystr = "hello world and itcast and itheima and Python "
# 結果: True
print(mystr.startswith('hello'))
# 結果False
print(mystr.startswith('hello', 5, 20))

"""
endswith()::檢查字元串串是否是以指定⼦子串串結尾,是則傳回 True,否則傳回 False。如果設定開
始和結束位置下标,則在指定範圍内檢查。
字元串串序列列.endswith(⼦子串串, 開始位置下标, 結束位置下标)
"""
mystr = "hello world and itcast and itheima and Python"
# 結果: True
print(mystr.endswith('Python'))
# 結果: False
print(mystr.endswith('python'))
# 結果: False
print(mystr.endswith('Python', 2, 20))

"""
判斷字母
isalpha():如果字元串串⾄至少有⼀一個字元并且所有字元都是字⺟母則傳回 True, 否則傳回 False。
"""
mystr1 = 'hello'
mystr2 = 'hello12345'
# 結果: True
print(mystr1.isalpha())
# 結果: False
print(mystr2.isalpha())


"""
判斷數字
isdigit():如果字元串串隻包含數字則傳回 True 否則傳回 False
"""
mystr1 = 'aaa12345'
mystr2 = '12345'
# 結果: False
print(mystr1.isdigit())
# 結果: False
print(mystr2.isdigit())


"""
判斷特殊字元
isalnum():如果字元串串⾄至少有⼀一個字元并且所有字元都是字⺟母或數字則返 回 True,否則傳回
False。
"""
mystr1 = 'aaa12345'
mystr2 = '12345-'
# 結果: True
print(mystr1.isalnum())
# 結果: False
print(mystr2.isalnum())


"""
判斷空白
isspace():如果字元串串中隻包含空⽩白,則傳回 True,否則傳回 False。
"""
mystr1 = '1 2 3 4 5'
mystr2 = ' '
# 結果: False
print(mystr1.isspace())
# 結果: True
print(mystr2.isspace())

           
上一篇: IP Networks

繼續閱讀