当前位置 : 主页 > 编程语言 > python >

Python爬虫-第一章-4-基础语法

来源:互联网 收集:自由互联 发布时间:2023-02-04
字符串格式化以及bool类型的特性 # Demo Describe:字符串格式化以及bool类型的特性 # # =================示例1,循环输出一句被格式化的字符串,输入空时自动停止循环========== # ''' # 1,bool类型:
  • 字符串格式化以及bool类型的特性
  • # Demo Describe:字符串格式化以及bool类型的特性# # =================示例1,循环输出一句被格式化的字符串,输入空时自动停止循环==========# '''# 1,bool类型:变量为null或者0时,默认false,其他时候默认true# 2,字符串格式化# '''# try:# while 1:# Name = input('请输入名字:')# Age = int(input('请输入年数:'))# if Name or Age :# content = f'{Name}我爱你.请你嫁给我好吗?爱你{Age}年!'# print(content)# else:# break## except ZeroDivisionError as e:# print(e)# # =================示例2,索引和切片==========# '''# 语法;[start:end:step]# 【开始,结束(不被计入切片范围),步长】# 开始或结束省略时,默认0或者最后# 步长:n个字符一组,输出首个字符,并且控制切片方向# '''# try:# content = '两仪式我爱你.请你嫁给我好吗?爱你一万年!'# print(content[0:5]) #两仪式我爱你# print(content[:5]) #两仪式我爱你# print(content[:]) #两仪式我爱你.请你嫁给我好吗?爱你一万年!# print(content[-7:-1]) #?爱你一万年# print(content[::2]) #江我你请嫁我吗爱一年# print(content[::-2]) # !万你?好给你.爱珊## except ZeroDivisionError as e:# print(e)# # =================示例3,常用处理==========# '''# 转换大小写# '''# try:# content = 'qWeR'# print(content.lower()) #qwer# print(content.upper()) #QWER# except ZeroDivisionError as e:# print(e)# # =================示例4,切割和替换==========# '''# strip,replace,split# '''# try:# content = ' 你好,赛 亚 人。 '# str=content.strip().replace(' ','') #你好,赛亚人。# print(str)# print(str.replace('赛亚人','周星驰')) # 你好,周星驰。# print(str.split('好,赛')) #['你', '亚人。'] 切割结果会存放到列表中 类似c# remove## except ZeroDivisionError as e:# print(e)# =================示例6,查找和判断和字符拼接=========='''查找 in find index'''try: # --------------查找 start--------------------------- # content = '两仪式我爱你.请你嫁给我好吗?爱你一万年!' # print('两仪式' in content) #True # print('两仪式' not in content) #False # print('两仪式' in content) #False # #in 可做存在判断,或循环 # content='两仪式我爱你' # for str in content: # print(str) # ------find,index 查找下标位置------------ # # find 返回所在位置的起点下标,-1代表未找到 # print(content.find('两仪式')) # 0 # print(content.find('两仪式')) # -1 # # find 返回所在位置的起点下标,未找到时进行异常报告 # print(content.index('两仪式')) # 0 # print(content.index('两仪式')) # ValueError: substring not found # --------------查找 end--------------------------- # # --------------判断 start--------------------------- # content = '两仪式我爱你.请你嫁给我好吗?爱你一万年!' # print(content.startswith('两仪式')) #是否以某字符开头 # print(content.isdigit()) #是否是整数 # print(len(content)) #输出字符长度 # #...等等 # # # --------------查找 end--------------------------- # --------------判断 字符拼接--------------------------- content = '两仪式,我爱你.请,你嫁给,我好吗?爱,你一,万年!' print(content.split(',')) # ['两仪式', '我爱你.请', '你嫁给', '我好吗?爱', '你一', '万年!'] str = content.split(',') print('_'.join(str)) # 两仪式_我爱你.请_你嫁给_我好吗?爱_你一_万年! # --------------查找 end---------------------------except ZeroDivisionError as e: print(e)
  • 元组
  • # Demo Describe:元组'''tuple-内部元素不可变数组 用 () 表示,除此外与列表使用方式相同'''try: # ------1,tuple只存放一个元素时需要加,号,区别于数学运算上的括号---------- content = ("元组",) print(content, type(content)) # ('元组',) <class 'tuple'> # ------2,tuple中的可变列表可以进行改变,本质上是内存地址固定---------- content = ("元组", "元组1", ['1', '2']) print(content, type(content)) # ('元组', '元组1', ['1', '2']) <class 'tuple'> content[2].append('3333') print(content, type(content)) # ('元组', '元组1', ['1', '2', '3333']) <class 'tuple'>except ZeroDivisionError as e: print(e)
  • while循环
  • # Demo Describe:while循环# ========示例1:1至10的总和i = 1totle = 0while i <= 10: totle = totle + i print('i值:' + str(i)) print('合计:' + str(totle)) i = i + 1
  • 转义字符
  • # Demo Describe:转义字符print('Hi!\tNice!') # tableprint('Hi!!1\tNice!')print('Hi!!123\tNice!')print('Hi!\nNice!') # new lineprint('Hi!\rNice!') # returnprint('Hi!\bNice!') # backspaceprint('SomeOne have \'good News!\'')print('SomeOne have \'\\\\good News!\'')print(r'Hi!\tNice!') # raw string --> don't translate anything#执行Hi! Nice!Hi!!1 Nice!Hi!!123 Nice!Hi!Nice!Nice!HiNice!SomeOne have 'good News!'SomeOne have '\\good News!'Hi!\tNice!

    网友评论