Python进阶
for不能对数字次数进行循环,要写成for i in range(5)
1.float():该函数只能用于将纯数字的字符串转化为浮点型数字;
x = float('111')
x = float('11.1') # 报错
2.字符串:
(1)字符串按照索引切片
# 索引切片
msg = 'hello lucy'
# 0123456789 # 索引序号
print(f'切片3-最后: {
msg[3:]}') # lo lucy
print(f'切片3-8: {
msg[3:8]}') #lo lu
print(f'切片3-8,步长为2: {
msg[3:8:2]}') # l u
print(f'切片3-最后,步长为2: {
msg[3::2]}') # l uy
#切片只包括切片左边界,不包括右边界
# 了解,步长为正从左到右;步长为负从右到左
print('\n**了解知识点**')
print(f'切片所有: {
msg[:]}') #hello lucy
print(f'反转所有: {
msg[::-1]}') #ycul olleh
print(f'切片-5--2: {
msg[-5:-2:1]}') #lu
print(f'切片-2--5: {
msg[-2:-5:-1]}') #cul
(2)字符串内置函数:strip()函数:用来移除字符串中的空格;若括号中指明其他字符(‘xx’),也可以一并移除;
# strip()应用场景
pwd = input('password: ') # 用户可能会手抖输入空格
if pwd.strip() == '123':
print('密码输入成功')
print(f"'*-& lucy+'.strip('*-& +'): {
'*-& lucy+'.strip