参看抖音程序员zhenguo,作为学习记录,以备查询
#字符串创建方法1
s= 'python'
print('that isn\'t a horse')
that isn't a horse
#字符串创建方法2
s= "python"
print("that isn't a horse")
that isn't a horse
#字符串创建方法3
print('''you're just pounding two
coconut halves together.1111''')
print("""you're just pounding two
coconut halves together.2222""")
you're just pounding two
coconut halves together.1111
you're just pounding two
coconut halves together.2222
# 根据身份证号判断性别
def f(id: str):
if int(id[-2])%2 == 1 :
print('男')
else:
print('女')
f('330652199610064721')
女
# 字符串与数字
3*'py'
'pypypy'
20*'-'
'--------------------'
'Py''thon'
'Python'
ord('A')
65
chr(97)
'a'
sb = bytes('adfad', encoding='utf-8')
sb
b'adfad'
type(sb)
bytes
for c in sb:
print(c)
97
100
102
97
100
sb.decode(encoding='utf-8')
'adfad'
# 打印格式
"This is {}".format('python')
'This is python'
"{:.3f}".format(3.1415926)
'3.142'
"{:0>3d}".format(3)
'003'
"{:.1%}".format(0.7189)
'71.9%'
"{:.4e}".format(10241024)
'1.0241e+07'
# 华氏度转为摄氏度
f = float(input('请输入华氏温度:'))
c = (f - 32)/1.8
print('%.1f华氏度=%.1f摄氏度' % (f,c))
请输入华氏温度:97.8
97.8华氏度=36.6摄氏度
# 字符串拼接join方法
' '.join(['I','LOVE','PYTHON'])
'I LOVE PYTHON'
# 字符串拆分split
'I LOVE PYTHON'.split(' ')
['I', 'LOVE', 'PYTHON']
# 字符串替换replace
'adadfa'.replace('a','')
'ddf'
# 字符串去除空格
' \n \tI love python \n\n'.strip()
'I love python'