1.try...except, else, finally的使用
data = 1
try:
if data == 1:
raise ZeroDivisionError
except ZeroDivisionError:
data = 0
else:
data = 10
finally:
print("Finally")
print(data)
结果
0
2.字符串格式化输出:
a. 字符串中的center,rjust, ljust
print("age".center(10, '*')) # 居中对齐
print("10".center(10, '*'))
结果
***age****
****10****
print("age".rjust(10, '*')) # 向右对齐
print("10".rjust(10, '*'))
结果
*******age
********10
print("age".ljust(10, '*')) # 向左对齐
print("10".ljust(10, '*'))
结果
age*******
10********
b. 字符串中format方法的使用(带对齐方式,宽度,填充字符)
name = 'xiaozhng'
age = 20
money = 99999999
print("My name is {:*^10} My age is {:*^10} My money is {:*^13}".format(name, age, money))
结果
My name is *xiaozhang* My age is ****20**** My money is **99999999**
c. 占位符: %d, %s, %f
print("My name is %s My age is %d My money is %.3f" % ('xiaozhang', 20, 999999.99))
结果
My name is xiaozhang My age is 20 My money is 999999.99
d. 新的格式化: f/F(带对齐方式,宽度,填充字符)
name = 'xiaozhang'
age = 30
print(f"My name is {name:*^10} My age is {age}")
结果
My name is *xiaozhang* My age is 30
3.字符串剩余方法的使用
capitalize
data = 'hello'
print(data.capitalize())
结果
Hello
casefold
data = 'HELLO'
print(data.capitalize())
结果
Hello
center
print("age".center(10, '*')) # 居中对齐
print("10".center(10, '*'))
结果
***age****
****10****
rjust
print("age".rjust(10, '*')) # 向右对齐
print("10".rjust(10, '*'))
结果
*******age
********10
ljust
print("age".ljust(10, '*')) # 向左对齐
print("10".ljust(10, '*'))
结果
age*******
10********
count
data = 'abcabcabc'
print(data.count('a'))
data = 'abcabcabc'
print(data.count('abc'))
结果
3
3
encode
data = 'abc'
print(data.encode('UTF-8'))
结果
b'abc'
decode
data = b'abc'
print(data.decode('UTF-8'))
结果
abc
endswith
data = 'abcabcabc'
print(data.endswith('c'))
data = 'abcabcabc'
print(data.endswith('bc'))
data = 'abcabcabc'
print(data.endswith('abc'))
结果
True
True
True
expandtabs
data = 'abc\tabc'
print(data.expandtabs(tabsize=9))
结果
abc abc
find
data = 'abcabcabc'
print(data.find('b'))
结果
1
format
name = 'xiaozhang'
age = 20
money = 99999999
print("My name is {:*^10} My age is {:*^10} My money is {:*^13}".format(name, age, money))
结果
My name is *xiaozhang* My age is ****20**** My money is **99999999**
format_map
data = 'abcabc666'
data1 = 'abcd'
print(data1.format_map(data))
print(data1)
结果
abcd
abcd
index
data = 'abcabcdabcd'
print(data.index('a'))
结果
0
isalnum
data = 'abcabcdabcd123'
print(data.isalnum())
data = 'abcabcdabcd123-'
print(data.isalnum())
结果
True
False
isalpha
data = 'abcabcdabcd123'
print(data.isalpha())
data = 'abcabcdabcd'
print(data.isalpha())
结果
False
True
isascii
data = 'abcabcdabcd'
print(data.isascii())
data = '哈哈'
print(data.isascii())
结果
True
False
isdecimal
data = 'x666'
print(data.isdecimal())
data = '666'
print(data.isdecimal())
结果
False
True
iadigit
data = '666'
print(data.isdigit())
data = '666abc'
print(data.isdigit())
结果
True
False
isidentifier
data = '666abc'
print(data.isidentifier())
data = 'abc666'
print(data.isidentifier())
结果
False
True
*补充 python标识符
islower
data = 'abc'
print(data.islower())
data = 'ABC'
print(data.islower())
结果
True
False
isnumeric
data = '123'
print(data.isnumeric())
data = '123a'
print(data.isnumeric())
结果
True
False
isprintable
data = 'abc\nabc'
print(data.isprintable())
data = 'abcabc'
print(data.isprintable())
结果
False
True
isspace
data = ' '
print(data.isspace())
data = 'a'
print(data.isspace())
结果
True
False
istitle
data = 'Money'
print(data.istitle())
data = 'MONEY'
print(data.istitle())
data = 'money'
print(data.istitle())
结果
True
False
False
isupper
data = 'ABC'
print(data.isupper())
data = 'abc'
print(data.isupper())
结果
True
False
join
print('-'.join(['bc', 'de']))
结果
bc-de
lower
data = 'ABC'
print(data.lower())
结果
abc
lstrip
data = ' abc'
print(data.lstrip(), data)
结果
abc abc
partition
data = 'ab|cd'
print((data.partition('|')))
结果
('ab', '|', 'cd')
removeprefix
data = 'abc'
print(data.removeprefix('a'))
data = 'abc'
print(data.removeprefix('ab'))
结果
bc
c
removesuffix
data = 'abc'
print(data.removesuffix('c'))
data = 'abc'
print(data.removesuffix('bc'))
结果
ab
a
replace
data = 'abc'
print(data.replace('c', 'r'))
结果
abr
rfind
data = 'abcabc'
print(data.rfind('a'))
结果
3
rindex
data = 'abcabca'
print(data.rindex('a'))
结果
6
rpartition
data = 'a|c'
print(data.rpartition('|'))
结果
('a', '|', 'c')
rsplit
data = 'a|c'
print(data.rsplit(sep='|'))
结果
['a', 'c']
split
data = 'abc'
print(data.split(sep='b'))
结果
['a', 'c']
splitlines
data = 'abc\nabc'
print(data.splitlines())
结果
['abc', 'abc']
startswith
data = 'abc'
print(data.startswith('a'))
data = 'abc'
print(data.startswith('b'))
结果
True
False
strip
data = ' abc '
print(data.strip())
print(data)
结果
abc
abc
swapcase
data = 'abcABC'
print(data.swapcase())
结果
ABCabc
title
data = 'ABCabc'
print(data.title())
结果
Abcabc
translate
data = 'abcabc'
print(data.translate({ord('a'): 'A'}))
结果
AbcAbc
upper
data = 'abcAbc'
print(data.upper())
结果
ABCABC
zfill
data = 'abc'
print(data.zfill(10))
结果
0000000abc
4.使用输入完成计算器的功能:
输入 7+8 输出: 7 + 8 = 15
提示:在字符串取查找是否有"+/-*"
找到字符串中有一个split方法:按照指定字符分割字符串
获取到 两个数字,以及运算符号
data = input("请输入加法运算")
if data.count('+') == 1:
num = data.split("+")
x = int(num[0])
y = int(num[1])
print(x + y)
else:
print('请输入加法运算')
结果
请输入加法运算7+8
15