python学习day6 for循环 字符串的内置方法

时间:2024-07-15 09:37:38

1.for循环

和while相比

l=[1,2,3]

i=0

while i <len(l)

  print(l[i])

  i+=1

l=['a','b','c']

for item in l:

  print(item)

字典中的应用:

  dic={'x':111,'y':222,'z':333}

  for k in dic:

    print(k,dic[k])

while循环称为条件循环,循环的次数取决于条件何时为false

for称为迭代器循环,循环的次数取决于数据包含的元素的个数

2.for循环专门用来取值,在循环方面比while循环强大,以后凡是遇到循环取值的场景,就应该用for循环

l=[1,2,3]

for i in range(3)

  print(i,l[i])

for+break

  names=['egon','kevin','alex','hulaoshi']

  for name in names:

    if name == 'alex':break

    print(name)

for+continue

  names=['egon','kevin','alex','hulaoshi']

  for name in names:

    if name == 'alex':continue

    print(name)

for+else 和while else相似,只有for循环没有被break结束的情况下会运行else内的代码

# for+else
# names=['egon','kevin','alex','hulaoshi']
# for name in names:
# # if name == 'alex':break
# print(name)
# else:
# print('=====>') for循环的嵌套
  for i in range(3):
    for j in range(2):
      print(i,j) print默认在结尾会换行,可以使用print(‘ ’,end='')使得输入内容后不换行 2.数据类型补充
整型int
定义 age=10 age=int(10)
类型转换
int(3.1)
int('3333') 特点:不可变类型
存一个值 浮点型float
float('11111.1)
特点:不可变类型
存一个值
x=11.1
id(x)
x=11.2
id(x) 了解内容**
十进制转换成其他进制
bin(13)
oct(13)八进制
hex(13)十六进制 3.字符串类型str:用来记录描述性的状态,比如名字,性别等
类型转换
res1=str(10)
res2=str(22.2)
res3=str(['a','b'])
res4=str({'x'}:1,'y':2)
type(res4)    3.2常用操作+内置方法
  优先掌握(*****)
  1.按索引取值(正向取+反向取):只能取字符,不能改
  msg='hello world'
  print(msg[-1])
  msg[0]='H'(会报错)
  2.切片(顾头不顾尾,步长)
  msg=‘hello world'
  print(msg[0:5])
  print(msg[0:5:2])从第一位到第五位字符,隔一个取一个
  print(msg[0:])从头取到尾
  print(mag[:])
  反向取
  print(msg[-1:-5:-1])从最后一位反向取到倒数第四位
  print(msg[::-1])反转字符串
  3.长度len:统计的是字符的个数
  msg=‘h长a’
  len(msg)
  4.成员运算符 in not in 判断一个子字符是否存在于一个大字符串中
  msg='hello world'
  print('ho' in msg)
  print('ho' not in msg)
  5.移除空白strip:移除字符串左右两边的某些字符
  msg=‘ hello world '
  print(msg.strip(' '))
  print(msg.strip())
  print(msg)(不直接修改字符串)

# name=input('name>>>: ').strip() #name='egon'
# pwd=input('password>>>: ').strip()
#
# if name == 'egon' and pwd == '123':
# print('login successfull')
# else:
# print('username or password error') # msg='***h**ello**********'
# print(msg.strip('*')) # msg='*-=+h/ello*(_+__'
# print(msg.strip('*-=+/(_'))
6.切分split:把有规律的字符串切成列表从而方便取值
info=‘egon:18:180:150’
res=info.split(‘:’,1)
print(res)
print(res[1]) s1=':'join(res)
s1=res[0]+':'+res[1]+':'+res[2]+....
':'.join([1,2,3,4,5]) 会报错,符号和数字没法相加
7.循环
  for i in 'hello':
    print(i) 需要掌握的操作(****)
1.strip,lstrip,rstrip
msg='****hello*****'
msg.strip('*')两边都去除*
msg.lstrip('*')从左边开始去除*
msg.rstrip('*')从右边开始去除*
2.lower,upper
msg='AaBbCcDdEe'
msg.lower()全部转换成小写
msg.upper()全部转换成大写
3.starswith,endswith
msg='alex is sb'
msg.startswith('al')字符串是否是al开头
msg.endswith('sb')字符串是否sb结尾,返回布尔值
4.format的三种玩法
msg=‘my name is %s,my age is %s'%('egon',18)
print(msg)
msg=‘my name is {name},my age is {age}'.format(name='egon',age=18)
msg=‘my name is {},my age is {}'.format('egon',18)
msg=‘my name is {0}{0},my age is {1}{1}{1}'.format('egon',18) 可以按数字对应后续的变量,多次引用变量
5.split,rsplit
cmd='get|a.txt|333'
print(cmd.split('|',1))
print(cmd.rsplit('|',1))从右开始分割字符串
6.replace
msg=‘kevin is sb kevin kevin'
print(msg.replace('kevin','sb',2))
7.isdigit 当字符串中为纯数字时结果为true
res=‘11111’(有小数点也不算是纯数字)
print(res.isdigit())
int(res)
# age_of_bk=18
# inp_age=input('your age: ').strip()
# if inp_age.isdigit():
# inp_age=int(inp_age)
# if inp_age > 18:
# print('too big')
# elif inp_age < 18:
# print('to small')
# else:
# print('you got it')
# else:
# print('必须输入纯数字')
了解的内容(**)
1.find,rfind,index,rindex取到搜索的值的索引值,找到返回索引值,没找到find返回-1,index报错
# print('xxxkevin is sb kevin'.find('kevin'))
# print('xxxkevin is sb kevin'.index('kevin'))
# print('xxxkevin is sb kevin'.rfind('kevin'))
# print('xxxkevin is sb kevin'.rindex('kevin')) 2.count,计算子字符串在大字符串中出现几次
# print('kevin is kevin is kevin is sb'.count('kevin'))
3.center,ljust,rjust,zfill 用字符填补空白
# print('egon'.center(50,'*'))居中显示‘egon’,总共显示50个字符,空白用*号填补
# print('egon'.ljust(50,'*')左对齐显示,空白用*显示
# print('egon'.rjust(50,'*'))
# print('egon'.zfill(50))左边用0填补

#4、captalize,swapcase,title
# print('my name is kevin'.capitalize())把字符串第一个字母改成大写
# print('AaBbCc'.swapcase())大写变小写,小写变大写
# print('my name is kevin'.title())把每个单词的首字母大写 #4、is其他
# name='egon123'
# print(name.isalnum()) #判断字符串由字母或数字组成
# print(name.isalpha()) #判断字符串只由字母组成 # print(name.islower())是否全小写
# print(name.isupper())
# name=' '
# print(name.isspace())是否是空格
msg='I Am Egon'
print(msg.istitle())是否每个单词首字母大写