python基础知识--2字符串
1.字符串操作
1.字符串连接
print('A'+'B')
运行结果:AB
2.字符串多次连接
print('Python ' * 3)
运行结果:Python Python Python
3.字符串切片操作
print('Python'[0])
运行结果:P
4.strip()
# 去除空白字符
print(' abc123abc '.strip())
运行结果:abc123abc
# 去除指定字符
print('123abc123'.strip('21'))
运行结果:3abc123
print('123abc321'.strip('213'))
运行结果:abc
print('4123abc123'.strip('21'))
运行结果:'4123abc123'
#lstrip用于去除最左边的字符
print(' abc123 '.lstrip())
运行结果:abc123
rstrip用于去除最右边的字符
print(' abc123 '.rstrip())
运行结果:abc123
判断字符串开头结尾字符
print('apple'.startswith('a'))
运行结果:True
返回字符位置
print('apple'.find('m'))
运行结果:-1
字符串替换
print('lemon'.replace('lemon','apple'))
运行结果:apple
字符串其他操作
print(len('apple'))
运行结果:5
print('lemonmmm'.count('m'))
运行结果:4
Print('apple'.upper())
运行结果:APPLE
print('APPLE'.lower())
运行结果:apple
print('apple'.center(10,'-'))
运行结果:--apple---
print('apple'.center(4,'-'))
运行结果:apple
print('applepear'.isalpha())
运行结果:True
print('123456'.isdigit())
运行结果:True
print('abc123'.isalnum())
运行结果:True
% 格式化字符串
print('hello %s,%s' %('lemon','python'))
运行结果:hello lemon,python
print('i am %d years old' % (16))
运行结果:i am 16 years old
print('%.5f' % (3.14))
运行结果:3.14000
print('i am %(name)s, i am %(age)s years old' %{'name':'lemon','age':16})
运行结果:i am lemon, i am 16 years old
format格式化字符串
print('hello {}'.format('world'))
运行结果:hello world
print('hello {1} {0}'.format('lemon','python'))
运行结果:hello python lemon
print('i am {name}, i am {age} years old'.format(name = "python",age = 16))
运行结果:i am python, i am 16 years old
Number(数字):整型,浮点型与复数及布尔类型
整型
# bin 是向二进制转化 0b1010
# oct 是向八进制转化 0o12
# int 是向十进制转化 10
# hex 是向十六进制转化 0xa
浮点型
复数
posted on 2020-10-07 22:02 kunlungege 阅读(144) 评论(0) 编辑 收藏 举报