字符串
eval(str)
- 功能:將字符串str當成有效的表達式來求值,並返回計算結果
num1 = eval("123")
print("num1 =", num1)
print(type(num1))
print(eval("+123"))
print(eval("-123"))
print(eval("12+3"))
print(eval("12-3"))
'''
結果爲:
num1 = 123
<class 'int'>
123
-123
15
9
'''
len(str)
print(len("you are a good boy!"))
str.lower()
str.upper()
print("you are a good man")
print("you are a good man".upper())
'''結果爲
you are a good man
YOU ARE A GOOD MAN
'''
str.swapcase()
- 轉換字符串中大寫字母爲小寫字母,小寫字母爲大寫字母。
print("You Are A Good Man".swapcase())
str.capitalize()
print("yoU ArE a gOOD mAN".capitalize())
str.title()
print("yoU ArE a gOOD mAN".title())
str.center(width[, fillchar])
- 返回一個指定寬度的居中的字符串,fillchar爲填充的字符,默認空格填充
print("you are a good man".center(40, "*"))
str.ljust(width[, fillchar])
- 返回一個指定寬度的左對齊的字符串,fillchar爲填充的字符,默認空格填充
str.rjust(width[, fillchar])
- 返回一個指定寬度的右對齊的字符串,fillchar爲填充的字符,默認空格填充
str.zfill(width)
- 返回一個指定長度爲width的字符串,原字符串右對齊,前面補0
str.count(str2[, start][, end])
- 返回字符串str中字符串str2出現的次數,可以指定一個範圍,默認從頭到尾
str.find(str2[, start][,end])
- 從左至右檢測str2字符串是否包含在字符串str中,可以指定一個範圍,默認從頭到尾,返回的是第一次出現的下標開始,未檢測到返回-1
str.rfind(str2[, start][,end])
- 從右至左檢測str2字符串是否包含在字符串str中,可以指定一個範圍,默認從頭到尾,返回的是第一次出現的下標開始,未檢測到返回-1
str.index(str2, start=0, end=len(str))
- 和find()方法一樣,只不過str2不存在的時候會報異常而不是像find()返回-1
str.rindex(str2, start=0, end=len(str))
- 和rfind()方法一樣,只不過str2不存在的時候會報異常而不是像rfind()返回-1
str.lstrip()
str.rstrip()
str.strip()
- 截掉字符串str兩側指定的字符,默認爲空格,指定num,則僅截取num個字符串
str.split(str1, num)
str1 = "you**are****a**good**man"
print(str1.split("*", 4))
# 結果爲:['you', '', 'are', '', '**a**good**man']
str.splitlines([keepends])
- 按照(‘\r’,‘\r\n’,‘\n’)分割,keepends=True會保留換行符,默認爲False
str1 = '''hello!
nice to meet you!
where are you from?'''
print(str1.splitlines())
print(str1.splitlines(True))
'''結果爲:
['hello!', 'nice to meet you!', 'where are you from?']
['hello!\n', 'nice to meet you!\n', 'where are you from?']
'''
str.join(seq)
- 以指定的字符串str分隔符,將列表seq中的所有元素組合成一個字符串
list1 = ["hello", "world", "Python", "py"]
str1 = "&".join(list1)
print(str1)
str2 = "-&-".join(list1)
print(str2)
'''結果爲:
hello&world&Python&py
hello-&-world-&-Python-&-py
'''
max()、min()
str1 = "you are a good man"
print(min(str1))
print(max(str1))
'''結果爲:
y
'''
str.replace(oldstr, newstr, count) 替換字符串
- 將字符串str中的字符串oldstr替換爲字符串newstr,默認全部替換。如果指定了count,那麼只替換前count個字符串oldstr爲字符串newstr
str0.maketrans(str1, str2)創建一個字符串映射表
str.translate(str0) 轉換字符串
str0 = str.maketrans("ac", "13")
str1 = "you are a good man"
str2 = str1.translate(str0)
print(str2)
# 結果爲:you 1re 1 good m1n
str.startswith(str1, start=0, end=len(str))
- 在給定的範圍內,字符串str是否以字符串str1開頭,如果沒有指定範圍,默認整個字符串
str.endswith(str1, start=0, end=len(str))
- 在給定的範圍內,字符串str是否以字符串str1結尾,如果沒有指定範圍,默認整個字符串
str1 = "you are a good man祺"
str2 = str1.encode("utf-8")
print(str2)
print(type(str1))
print(type(str2))
str3 = str2.decode("utf-8")
print(str3)
print(type(str3))
'''結果爲:
b'you are a good man\xe7\xa5\xba'
<class 'str'>
<class 'bytes'>
you are a good man祺
<class 'str'>
'''
str.isalpha()
- 如果字符串至少有一個字符且所有字符都是字母返回True,否則返回False
str1 = "you are a good man祺"
str2 = "you are a good man"
str3 = "youareagoodman"
print(str1.isalpha())
print(str2.isalpha())
print(str3.isalpha())
'''結果爲:
False
False
True
'''
str.isalnum()
- 如果字符串中至少有一個字符且所有字符都是字母或者數字返回True,否則返回False
str1 = "123"
str2 = "a1b2c3"
str3 = "a1#b2c3"
str4 = "a1 b2c3"
print(str1.isalnum())
print(str2.isalnum())
print(str3.isalnum())
print(str4.isalnum())
'''結果爲:
True
True
False
False
'''
str.isupper()
- 如果字符串中至少有一個英文字符且所有的英文字符都是大寫的英文字母返回True,否則返回False
print("ABC".isupper())
print("ABC1".isupper())
print("ABC#".isupper())
print("1".isupper())
print("AaBC".isupper())
print("AaBC#".isupper())
'''結果爲:
True
True
True
False
False
False
'''
str.islower()
- 如果字符串中至少有一個英文字符且所有的英文字符都是小寫的英文字母返回True,否則返回False
str.istitle()
- 如果字符串是標題化的返回True,否則返回False
print("Hello World".istitle())
print("Hello world".istitle())
print("hello world".istitle())
print("Hello".istitle())
print("hello".istitle())
'''結果爲:
True
False
False
True
False
'''
str.isdigit()
str.isnumeric()
- 如果字符串中只包含數字字符返回True,否則返回False
print("".isdigit())
print("1".isdigit())
print("1a".isdigit())
print("1 2".isdigit())
'''結果爲:
False
True
False
False
'''
str.isdecimal
- 字符串中只包含十進制字符返回True,否則返回False
str.isspace()
- 如果字符串中只包含空格返回True,否則返回False