导读:
本文主要记录自己学习python3基础字符串中的要点知识和例子;仅供自己梳理。
# 数字类型
a = 100
# 字符串类型
b = "hello world"
c = '''hello
world
'''
print(c)
d = """hello
world
"""
print(d)
print("wsd's name is ydxqbjl")
print('wsd"s name is fydxqbjl')
print('fydxqbjl ' + ' is ' + ' wsd')
\ | 转义符 |
---|---|
\n | 换行符 |
\t | 横向制表符 |
\ | 续反斜杠符号 |
\r | 回车 |
%s | 字符串占位符 |
---|---|
%d | 数字占位符 |
%f | 浮点型数字占位符 |
%.2f | 控制浮点型数字占位符 |
print('my name is %s'%('ydxqbjl'))
print('my name is %s ,my age is %d,my height is %f'%('ydxqbjl',18,1.80))
print('my name is %s ,my age is %d,my height is %.2f'%('ydxqbjl',18,1.80))
print(len('asdfgsafd'))
a = 'abcdefghi'
# 获取第二个
print(a[1])
# [start:end]切片,包头不包尾
print(a[1:3])
# [start:end:step],步长为step-1
print(a[1:5:2])
#
print(a[-1])
print(a[-3])
# 从左到右,不是a[-1:-3]
print(a[-3:-1])
# start默认为0,end默认为最后一个下标
print(a[:8])
print(a[3:])
# 输出全部
print(a[::])
# 翻转输出
print(a[::-1])
print(a[-5:-9:-1])
print(a[4:0:-1])
data = 'hello world'
# 从左往右
print(data.count('l'))
#
print(data.find('e'))
# 从右往左
print(data.rfind('e'))
#
print(data.index('e'))
data = 'hello world'
# 返回元组
print(data.partition('o'))
#
data_line = '''I
am
ydxq
'''
# 返回列表
print(data_line.splitlines())
#
## split,返回列表
print(data.split('o'))
data = 'hello world'
print(data.replace('h','w'))
data2 = 'ydxq'
print(data2.center(10))
data = ' ydxq '
print(data.strip())
# 第一种
print(f'my name is {data2}')
# 第二种
print('my name is {}'.format('ydxq'))
# 第三种,骚操作
print('my name is {} my age is {}'.format('ydxq',18))
print('my name is {1} my age is {0}'.format('ydxq',18))
# 第四种,最骚操作
print('my name is {name} my age is {age}'.format(name = 'ydxq',age = 18))
print('hello'.upper())
print('HELLO'.lower())
print('hello world'.title())
print('1234'.isdigit())
print('1234a'.isdigit())
print('ydxqbjl is niupi'.startswith('y'))
print('ydxqbjl is niupi'.endswith('niupi'))
data3 = 'ydxqbjl'
print('f' in data3)
print('y' in data3)
print('f' not in data3)
print(ord('A'))
print(chr(65))
str = '金山云'
e = str.encode('utf-8')
print(e)
d = e.decode('utf-8')
print(d)
BMI指数是身体质量指数,BMI值计算公式: BMI = 体重(公斤) / 身高的平方(米)。
例如: 一个人69公斤,身高是173公分。BMI = 69 / 1.73**2 = 23.05
标准表:
BMI < 18.5 体重过轻
18.5 <= BMI < 24 体重正常
BMI > 24 体重过重
编程要求: 输入身高的体重,打印出BMI的值并打印体重状况
str_heigh = input('请输入您的身高(m):')
str_weight = input('请输入您的体重(kg):')
heigh = float(str_heigh)
weight = float(str_weight)
BMI = weight / heigh ** 2
if BMI < 18.5:
print('体重过轻,您的BMI值为:%.2f'%(BMI))
elif 18.5 <= BMI <24:
print('体重正常,您的BMI值为:%.2f'%(BMI))
elif 24 < BMI:
print('体重过重,您的BMI值为:%.2f'%(BMI))