目录
1.try...except, else, finally的使用
b. 字符串中format方法的使用(带对齐方式,宽度,填充字符)
endswith(...) 判断以字符串是否以指定后缀结尾(可以是字符串)
format_map(...) 使用data1格式化并替换data
isalnum(self, /)如果字符串中只有字符或数字(或字母和数字),则返回 True,否则返回 False。
isalpha(self, /)如果字符串是字母字符串,则返回 True,否则返回 False。
isascii(self, /)如果字符串中的所有字符都是 ASCII码表中的,则返回 True,否则返回 False。
isdecimal(self, /)如果字符串是十进制字符串,则返回 True,否则返回 False。
isdigit(self, /)如果字符串是数字字符串,则返回 True,否则返回 False。
isidentifier(self, /)如果字符串是有效的 Python 标识符,则返回 True,否则返回 False。
islower(self, /)如果字符串是小写字符串,则返回 True,否则返回 False。
isnumeric(self, /)如果字符串是数字字符串,则返回 True,否则返回 False。
isprintable(self, /)如果字符串可打印,则返回 True,否则返回 False。
isspace(self, /)如果字符串是空格字符串,则返回 True,否则返回 False。
istitle(self, /)如果字符串是标题大小写的字符串,则返回 True,否则返回 False。
isupper(self, /)如果字符串是大写字符串,则返回 True,否则返回 False。
join(self, iterable, /)连接任意数量的字符串。
lstrip(self, chars=None, /)返回删除了前导空格的字符串副本。
partition(self, sep, /)使用给定的分隔符将字符串划分为三个部分。
removeprefix(self, prefix, /)返回一个删除了给定前缀字符串(如果存在)的 str。
removesuffix(self, suffix, /)返回一个 str,如果存在,则删除给定的后缀字符串。
replace(self, old, new, count=-1, /)返回一个副本,其中包含所有出现的旧子字符串,该子字符串已替换为 new。
rindex(...) 返回 S 中找到子字符串子项的最高索引
rpartition(self, sep, /)使用给定的分隔符将字符串划分为三个部分。
rsplit(self, /, sep=None, maxsplit=-1)返回字符串中单词的列表,使用 sep 作为分隔符字符串。
split(self, /, sep=None, maxsplit=-1)返回字符串中单词的列表,使用 sep 作为分隔符字符串。
splitlines(self, /, keepends=False)
Return a list of the lines in the string, breaking at line boundaries.
如果 S 以指定的前缀开头,则返回 True,否则返回 False。
strip(self, chars=None, /)返回删除了前导和尾随空格的字符串副本。(删除前后空格)
swapcase(self, /)将大写字符转换为小写,将小写字符转换为大写。
title(self, /)返回字符串的一个版本,其中每个单词的标题都大写。(首字母大写,其余小写)
translate(self, table, /)使用给定的翻译表替换字符串中的每个字符。
zfill(self, width, /)在左侧用零填充数字字符串,以填充给定宽度的字段。
找到字符串中有一个split方法:按照指定字符分割字符串 获取到 两个数字,以及运算符号
1.try...except, else, finally的使用
data = 3
try:
if data == 1:
raise ValueError
except ValueError:
data = 2
else:
data = 4
# print(data)
finally:
print("Finally")
print(data)
D:\python\python.exe D:/python/python_code/as.py
Finally
4
Process finished with exit code 0
2.字符串格式化输出:
a. 字符串中的center,rjust, ljust
# """
# center:居中对齐
# rjust:右对齐
# ljust:左对齐
# # 居中对齐,左对齐,右对齐,前提都是要有宽度,以及剩余部分的填充字符
# """
print("姓名", "年龄", "性别")
print("小乔".center(10), "19".center(10), "男".center(10)) # center:居中对齐
print("小乔".rjust(10), "19".rjust(10), "男".rjust(10)) # rjust:右对齐
print("小乔".ljust(10), "19".ljust(10), "男".ljust(10)) # ljust:左对齐
D:\python\python.exe D:/python/python_code/as.py
姓名 年龄 性别
小乔 19 男
小乔 19 男
小乔 19 男
Process finished with exit code 0
b. 字符串中format方法的使用(带对齐方式,宽度,填充字符)
name = "xiaoqiao"
age = 19
print("My name is {:>8} My age is {}".format(name, name)) # 右对齐
print("My name is {:*>18} My age is {}".format(name, name)) # 填充字符
print("My name is {name} My age is {0}".format(age, name=name))
D:\python\python.exe D:/python/python_code/as.py
My name is xiaoqiao My age is xiaoqiao
My name is **********xiaoqiao My age is xiaoqiao
My name is xiaoqiao My age is 19
Process finished with exit code 0
c. 占位符: %d, %s, %f
print("My name is %s My age is %d My money is %.2f" % ('xiaoqiao', 19, 1000000000))
D:\python\python.exe D:/python/python_code/as.py
My name is xiaoqiao My age is 19 My money is 1000000000.00
Process finished with exit code 0
d. 新的格式化: f/F(带对齐方式,宽度,填充字符)
name = 'xiaoqiao'
age = '19'
print(f"My name is {name:*^10}My name is {age}")
D:\python\python.exe D:/python/python_code/as.py
My name is *xiaoqiao*My name is 19
Process finished with exit code 0
3.字符串剩余方法的使用
capitalize(self, /) 首字母大写
data = 'hello'
print(data.capitalize())
D:\python\python.exe D:/python/python_code/jiaqi.py
Hello
Process finished with exit code 0
casefold(self, /) 全部换成小写
data = 'HELLO'
print(data.casefold())
D:\python\python.exe D:/python/python_code/jiaqi.py
hello
Process finished with exit code 0
center 居中对齐
print("小乔".center(10), "19".center(10), "男".center(10)) # center:居中对齐
D:\python\python.exe D:/python/python_code/jiaqi.py
小乔 19 男
Process finished with exit code 0
rjust 右对齐
print("小乔".rjust(10), "19".rjust(10), "男".rjust(10)) # rjust:右对齐
D:\python\python.exe D:/python/python_code/jiaqi.py
小乔 19 男
Process finished with exit code 0
ljust 左对齐
print("小乔".ljust(10), "19".ljust(10), "男".ljust(10)) # ljust:左对齐
D:\python\python.exe D:/python/python_code/jiaqi.py
小乔 19 男
Process finished with exit code 0
count(...) 计数,统计字符或字符串的出现次数
data_1 = 'ajhdshjskafhakdjfhdahjsfgdhjsagfgagdfhad'
print(data_1.count('a'))
data_2 = 'abcabcabcabc'
print(data_2.count('abc'))
D:\python\python.exe D:/python/python_code/jiaqi.py
7
4
Process finished with exit code 0
encode(self, /, encoding='utf-8', errors='strict')字符串编码字节
Encode the string using the codec registered for encoding.
data = 'abc'
print(data.encode('UTF-8'))
D:\python\python.exe D:/python/python_code/jiaqi.py
b'abc'
Process finished with exit code 0
decode 解码(字节解码成字符串)
data = b'abc'
print(data.decode('UTF-8'))
D:\python\python.exe D:/python/python_code/jiaqi.py
abc
Process finished with exit code 0
endswith(...) 判断以字符串是否以指定后缀结尾(可以是字符串)
data = 'abcabcabc'
print(data.endswith('c'))
data = 'abcabcabc'
print(data.endswith('bc'))
data = 'abcabcabc'
print(data.endswith('abc'))
D:\python\python.exe D:/python/python_code/jiaqi.py
True
True
True
Process finished with exit code 0
expandtabs使原字符串中的制表符("\t")的使用空间变大,使用空格来扩展空间。tabsize 的默认值为8。tabsize值为0到7等效于tabsize=8。tabsize每增加1,原字符串中“\t”的空间会多加一个空格。
str = "i love\tpython"
print(str.expandtabs())#默认值为8
print(str.expandtabs(tabsize=8))
print(str.expandtabs())
print(str.expandtabs(2)) #tabsize值为0到7,与tabsize值为8相同
print(str.expandtabs(tabsize=2)) #可以直接写数字
print(str.expandtabs(tabsize=9)) #可以直接写数字
print(str.expandtabs(tabsize=10)) #可以直接写数字
D:\python\python.exe D:/python/python_code/jiaqi.py
i love python
i love python
i love python
i love python
i love python
i love python
i love python
Process finished with exit code 0
find 返回最小的下标
data = 'abcda'
print(data.find('a')) # 从下标0开始,查找在字符串里第一个出现的子串,返回结果:0
print(data.find('a', 1)) # 从下标1开始,查找在字符串里第一个出现的子串:返回结果4
print(data.find('b'))
print(data.find('c'))
print(data.find('d'))
print(data.find('e')) # 查找不到返回-1
D:\python\python.exe D:/python/python_code/jiaqi.py
0
4
1
2
3
-1
Process finished with exit code 0
format(...)格式化(占位)
name = 'xiaoqiao'
age = 19
money = 10000000000
print("My name is{:*>10}My age is{:*^12}My money is{:*<18}".format(name, age, money))
D:\python\python.exe D:/python/python_code/jiaqi.py
My name is**xiaoqiaoMy age is*****19*****My money is10000000000*******
Process finished with exit code 0
format_map(...) 使用data1格式化并替换data
data = 'abcd123'
data_1 = 'abc'
print(data_1.format_map(data))
print(data_1)
D:\python\python.exe D:/python/python_code/jiaqi.py
abc
abc
Process finished with exit code 0
index(...)返回 S 中找到子字符串子项的最低索引
data = 'abcabcdabcd'
print(data.index('a'))
D:\python\python.exe D:/python/python_code/jiaqi.py
0
Process finished with exit code 0
isalnum(self, /)如果字符串中只有字符或数字(或字母和数字),则返回 True,否则返回 False。
data = 'ahjdkjnaka612mxla'
print(data.isalnum())
data = 'adhjalkakdao-'
print(data.isalnum())
D:\python\python.exe D:/python/python_code/jiaqi.py
True
False
Process finished with exit code 0
isalpha(self, /)如果字符串是字母字符串,则返回 True,否则返回 False。
data = 'jskkajdada12233'
print(data.isalpha())
data = 'jdkxjkcejscjis'
print(data.isalpha())
D:\python\python.exe D:/python/python_code/jiaqi.py
False
True
Process finished with exit code 0
isascii(self, /)如果字符串中的所有字符都是 ASCII码表中的,则返回 True,否则返回 False。
data = 'abcabcdabcd'
print(data.isascii())
data = '汉子'
print(data.isascii())
D:\python\python.exe D:/python/python_code/jiaqi.py
True
False
Process finished with exit code 0
isdecimal(self, /)如果字符串是十进制字符串,则返回 True,否则返回 False。
data = 'djd246'
print(data.isdecimal())
data = '32'
print(data.isdecimal())
D:\python\python.exe D:/python/python_code/jiaqi.py
False
True
Process finished with exit code 0
isdigit(self, /)如果字符串是数字字符串,则返回 True,否则返回 False。
data = '72283'
print(data.isdigit())
data = 'jakhd723'
print(data.isdigit())
D:\python\python.exe D:/python/python_code/jiaqi.py
True
False
Process finished with exit code 0
isidentifier(self, /)如果字符串是有效的 Python 标识符,则返回 True,否则返回 False。
data = '728637ajja'
print(data.isidentifier())
data = 'kjaja7212'
print(data.isidentifier())
D:\python\python.exe D:/python/python_code/jiaqi.py
False
True
Process finished with exit code 0
#python 标识符命名规则
#Python 标识符由 26 个英文字母大小写,0-9 ,_ 组成。
#Python 标识符不能以数字开头。
#Python 标识符严格区分大小写。
#Python 标识符不能包含空格、@、% 以及 $ 等特殊字符。
#不能以系统保留关键字作为标识符(一共有25 个)。
islower(self, /)如果字符串是小写字符串,则返回 True,否则返回 False。
data = 'abc'
print(data.islower())
data = 'ABC'
print(data.islower())
D:\python\python.exe D:/python/python_code/jiaqi.py
True
False
Process finished with exit code 0
isnumeric(self, /)如果字符串是数字字符串,则返回 True,否则返回 False。
data = '123'
print(data.isnumeric())
data = '123a'
print(data.isnumeric())
D:\python\python.exe D:/python/python_code/jiaqi.py
True
False
Process finished with exit code 0
isprintable(self, /)如果字符串可打印,则返回 True,否则返回 False。
data = 'abc\nabc'
print(data.isprintable())
data = 'abcabc'
print(data.isprintable())
D:\python\python.exe D:/python/python_code/jiaqi.py
False
True
Process finished with exit code 0
isspace(self, /)如果字符串是空格字符串,则返回 True,否则返回 False。
data = ' '
print(data.isspace())
data = 'f'
print(data.isspace())
D:\python\python.exe D:/python/python_code/jiaqi.py
True
False
Process finished with exit code 0
istitle(self, /)如果字符串是标题大小写的字符串,则返回 True,否则返回 False。
data = 'Money'
print(data.istitle())
data = 'MONEY'
print(data.istitle())
data = 'money'
print(data.istitle())
D:\python\python.exe D:/python/python_code/jiaqi.py
True
False
False
Process finished with exit code 0
isupper(self, /)如果字符串是大写字符串,则返回 True,否则返回 False。
data = 'ABC'
print(data.isupper())
data = 'abc'
print(data.isupper())
D:\python\python.exe D:/python/python_code/jiaqi.py
True
False
Process finished with exit code 0
join(self, iterable, /)连接任意数量的字符串。
print('-'.join(['ab', 'cd']))
D:\python\python.exe D:/python/python_code/jiaqi.py
ab-cd
Process finished with exit code 0
lower(self, /)返回转换为小写的字符串的副本。
data = 'ABC'
print(data.lower())
D:\python\python.exe D:/python/python_code/jiaqi.py
abc
Process finished with exit code 0
lstrip(self, chars=None, /)返回删除了前导空格的字符串副本。
data = ' abc'
print(data.lstrip(), data)
D:\python\python.exe D:/python/python_code/jiaqi.py
abc abc
Process finished with exit code 0
partition(self, sep, /)使用给定的分隔符将字符串划分为三个部分。
data = 'ab|cd'
print((data.partition('|')))
D:\python\python.exe D:/python/python_code/jiaqi.py
('ab', '|', 'cd')
Process finished with exit code 0
removeprefix(self, prefix, /)返回一个删除了给定前缀字符串(如果存在)的 str。
data = 'abc'
print(data.removeprefix('a'))
data = 'abc'
print(data.removeprefix('ab'))
D:\python\python.exe D:/python/python_code/jiaqi.py
bc
c
Process finished with exit code 0
removesuffix(self, suffix, /)返回一个 str,如果存在,则删除给定的后缀字符串。
data = 'abc'
print(data.removesuffix('c'))
data = 'abc'
print(data.removesuffix('bc'))
D:\python\python.exe D:/python/python_code/jiaqi.py
ab
a
Process finished with exit code 0
replace(self, old, new, count=-1, /)返回一个副本,其中包含所有出现的旧子字符串,该子字符串已替换为 new。
data = 'abc'
print(data.replace('c', 'r'))
D:\python\python.exe D:/python/python_code/jiaqi.py
abr
Process finished with exit code 0
rfind(...)返回 S 中找到子字符串子项的最高索引
data = 'abcabc'
print(data.rfind('a'))
D:\python\python.exe D:/python/python_code/jiaqi.py
3
Process finished with exit code 0
rindex(...) 返回 S 中找到子字符串子项的最高索引
data = 'abcabca'
print(data.rindex('a'))
D:\python\python.exe D:/python/python_code/jiaqi.py
6
Process finished with exit code 0
rpartition(self, sep, /)使用给定的分隔符将字符串划分为三个部分。
data = 'a|c'
print(data.rpartition('|'))
D:\python\python.exe D:/python/python_code/jiaqi.py
('a', '|', 'c')
Process finished with exit code 0
rsplit(self, /, sep=None, maxsplit=-1)返回字符串中单词的列表,使用 sep 作为分隔符字符串。
data = 'a|c'
print(data.rsplit(sep='|'))
D:\python\python.exe D:/python/python_code/jiaqi.py
['a', 'c']
Process finished with exit code 0
split(self, /, sep=None, maxsplit=-1)返回字符串中单词的列表,使用 sep 作为分隔符字符串。
data = 'abc'
print(data.split(sep='b'))
D:\python\python.exe D:/python/python_code/jiaqi.py
['a', 'c']
Process finished with exit code 0
splitlines(self, /, keepends=False)
Return a list of the lines in the string, breaking at line boundaries.
返回字符串中在行边界处断开的行的列表。
data = 'abc\nabc'
print(data.splitlines())
D:\python\python.exe D:/python/python_code/jiaqi.py
['abc', 'abc']
Process finished with exit code 0
startswith(...)
S.startswith(prefix[, start[, end]]) -> bool
Return True if S starts with the specified prefix, False otherwise.
如果 S 以指定的前缀开头,则返回 True,否则返回 False。
data = 'abc'
print(data.startswith('a'))
data = 'abc'
print(data.startswith('b'))
D:\python\python.exe D:/python/python_code/jiaqi.py
True
False
Process finished with exit code 0
strip(self, chars=None, /)返回删除了前导和尾随空格的字符串副本。(删除前后空格)
data = ' abc '
print(data.strip())
print(data)
D:\python\python.exe D:/python/python_code/jiaqi.py
abc
abc
Process finished with exit code 0
swapcase(self, /)将大写字符转换为小写,将小写字符转换为大写。
data = 'abcABC'
print(data.swapcase())
D:\python\python.exe D:/python/python_code/jiaqi.py
ABCabc
Process finished with exit code 0
title(self, /)返回字符串的一个版本,其中每个单词的标题都大写。(首字母大写,其余小写)
data = 'ABCabc'
print(data.title())
D:\python\python.exe D:/python/python_code/jiaqi.py
Abcabc
Process finished with exit code 0
translate(self, table, /)使用给定的翻译表替换字符串中的每个字符。
data = 'abcabc'
print(data.translate({ord('a'): 'A'}))
D:\python\python.exe D:/python/python_code/jiaqi.py
AbcAbc
Process finished with exit code 0
upper(self, /)返回转换为大写的字符串的副本。
data = 'abcAbc'
print(data.upper())
D:\python\python.exe D:/python/python_code/jiaqi.py
ABCABC
Process finished with exit code 0
zfill(self, width, /)在左侧用零填充数字字符串,以填充给定宽度的字段。
data = 'abc'
print(data.zfill(10))
D:\python\python.exe D:/python/python_code/jiaqi.py
0000000abc
Process finished with exit code 0
4.使用输入完成计算器的功能:
输入 7+8 输出: 7 + 8 = 15
提示:在字符串取查找是否有"+/-*"
找到字符串中有一个split方法:按照指定字符分割字符串
获取到 两个数字,以及运算符号
data = input("请输入加法运算")
# print(data.count('+'))
if data.count('+') == 1:
num = data.split("+")
x = int(num[0])
y = int(num[1])
print(x + y)
else:
print('请输入加法运算')
D:\python\python.exe D:/python/python_code/jiaqi.py
请输入加法运算7+8
15
Process finished with exit code 0