一、字符串拼接
+
name = '木易'
age = '29'
print(name+age)
F:\Python3.9\python.exe "D:\muyi\my python\练习.py"
木易29
Process finished with exit code 0
join
a = '2023','05','20'
a = '-'.join(a)
print(a)
F:\Python3.9\python.exe "D:\muyi\my python\练习.py"
2023-05-20
Process finished with exit code 0
format
name = 'muyi'
age = 25
print('我叫{},今年{}岁'.format(name,age))
F:\Python3.9\python.exe "D:\muyi\my python\练习.py"
我叫muyi,今年25岁
Process finished with exit code 0
f 插值法
name = 'muyi'
age = 25
print(f'我叫{name},今年{age}岁')
F:\Python3.9\python.exe "D:\muyi\my python\练习.py"
我叫muyi,今年25岁
Process finished with exit code 0
二、编码和解码
utf-8:——默认编码格式
编码
name = '木易'
# 将木易转为 utf-8 格式的二进制编码
a = name.encode('utf-8')
print(a)
F:\Python3.9\python.exe "D:\muyi\my python\练习.py"
b'\xe6\x9c\xa8\xe6\x98\x93'
Process finished with exit code 0
解码
name = '木易'
# 将木易转为 utf-8 格式的二进制编码
a = name.encode('utf-8')
print(a)
# 解码 将二进制编码进行解码
b = a.decode('utf-8')
print(b)
F:\Python3.9\python.exe "D:\muyi\my python\练习.py"
b'\xe6\x9c\xa8\xe6\x98\x93'
木易
Process finished with exit code 0
例:已知 name=’xxx‘,age='20',请在控制台打印出 我叫什么?今年多少岁?我离60岁还有多少年,所以要好好学习
# 已知 name=’xxx‘,age='20'
# 请在控制台打印出 外交什么?几年多少岁?我离60岁还有多少年,所以要好好学习
name = '木易'
age = '20'
print(f'我叫{name},今年{age}岁,我离60岁还有{60-int(age)}年,所以要好好学习')
F:\Python3.9\python.exe "D:\muyi\my python\练习.py"
我叫木易,今年20岁,我离60岁还有40年,所以要好好学习
Process finished with exit code 0
例:已知 mylist = ['2020','10','03'],请在控制台打印 2020-10-03
# 已知 mylist = ['2020','10','03'],请在控制台打印 2020-10-03
mylist = ['2020','10','03']
a = '-'.join(mylist)
print(a)
F:\Python3.9\python.exe "D:\muyi\my python\练习.py"
2020-10-03
Process finished with exit code 0