python从零学(二)

参看抖音程序员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'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值