作业问题源代码为:
name=" aleX" str1=print(name.strip()) # 移除两边按空格 print(name.startswith("al"))#判断是否以“al"开头 print(name.endswith("X"))#判断是否以”X“结尾 str2=name.replace('l','p')#用l替换p print(str2)#打印结果 print(name.split("l"))#用l将name分割 print(name.upper())#将name变量变为大写 print(name.lower())#将name变量变为小写 print(name[1])#打印第二个字符 print(name[0:3])#输出前三个字符 print(name[3:5])#输出最后两个字符 print(name.find('e'))#输出e所在的索引位置 s="oldboy"
print(s[0:-1]#获取子序列,去掉最后一个字符
得到的结果如下:
aleX False True apeX [' a', 'eX'] ALEX alex a al eX 3
oldbo
列表内置对象练习内容:
list1=['tank',18,'male',3.0,9] print(list1[4]) print(list1[-2]) print(list1[1:5])#切片,顾头不顾尾 print(list1[1:5:2]) print(len(list1)) print('tank' in list1) print('tank' not in list1) list1.append("张全蛋")#在列表末尾追加 print(list1) # print(list1.pop()) #(-1)删除 pop取 # print(list1.pop(0)) level=list1.pop()#取出列表中的值并赋值给level变量 print(level) print(list1) name=list1.remove('tank')#直接删除列表中的'tank',把none赋值给name print(name) print(list1) # 循环 for line in list1: print(line)
字符串内置对象练习为
list1=['tank',18,'广东'] print(list1[2]) print(list1[-1]) str1=' my name is xxx,my age is 18 ' print(str1[3:7]) print(str1[3:10:2]) print('x' in str1) print(len(str1)) print(str1) print(str1.strip())# 去除左右两边的空格 str2='my name is xxx, my age is 18' list2=str2.split(' ') #for i in str2: # print(i,end='')# 此为自定义参数 str3 = 'my name is Tank' print(str3.lower()) print(str3.upper()) print(str3.startswith("my"))#判断字符串首尾是否为对应的值 print(str3.startswith("MY")) print(str3.endswith("Tank")) str4='my name is {}, my age is {}'#位置 str4=str4.format('tank',18,111) print(str4) str5='my name is {0}, my age is {1}'#索引 str5=str4.format('tank',18,111) print(str5) str5='my name is {name}, my age is {age}'#直接指定 str5=str4.format(name='tank',age=18) print(str5) name='tank' age="18" print(':'.join([name,age])) str6='old new' str6=str6.replace("old","python") print(str6) str7='***my name is xxx, my age is 18' print(str7.lstrip('**')) str8='my name is xxx, my age is 18***' print(str8.rstrip('**')) ID=input("请输入ID") print(ID) print(type(ID)) print(ID.isdigit())#判断字符串是否为数字
第一节所学基本知识
print("hello world") name='tank' print(name) name = input('input username') pwd = input('input password') print(name,pwd) print(type(name),type(pwd)) #for循环 list1=[1,2,3,4,5] for line in list1: print(line) for line in range(1,10): print(line) SCHOOL="ahpu"#(以大写字母规定的默认为常量) number = 100 # int(100)整型 number2 = int(100) sal = 15.0 # float(15.0)浮点型 sal2 = float(15.0) name = 'zmm'#字符串类型 print('zmm' + 'ahpu')#两个字符串相连 list1 = ['张嫚嫚','zmm', 11, 1.0, ['ahpu', 100]] print(list1[0]) print(list1[5][0]) dict1 = {'name': "tank", "age": 18} # {name="tank", "age"=18} print(dict1["name"])#键值对key,value #布尔类型 print(10 == 10) # True print(10 == 11) # False #默认布尔类型的值 if 0: print('111') if None: print('111') if []: print('111')
通过今天的学习,对python的基本内容进行了解,通过动手实践,加深了印象,其中一些学习理解添加在备注中