1.今日课堂内容总结
# print('hello world')
#
# # 变量值:'dake',会在内存中产生一份内存地址。
# # 变量名:相当于一个门牌号,用于与变量进行绑定。
# # =:用来把变量值绑定给变量。
# name = 'cocoa'
# print(name) # cocoa
#
# # 变量名规范 下划线命名
# age_of_cocoa = 17
#
# # 不要使用中文命名
# # 名字 = 'cocoa'
# # print(名字) # cocoa
#
# # 定义变量的三大特征
# # id:变量的值一样,内容地址是不一样的。
# name1 = 'cocoa1'
# name2 = 'cocoa1'
# # Python优化机制(小计数值)
# # 在某个长度内,Python把值相同的变量值统一存放在同一个内存地址中。
# print(id(name1))
# print(id(name2))
# # type:用于判断变量的类型
# str1 = 'hello'
# print(type(str1))
#
# # value
# str2 = 'hello'
# print(str1 == str2)
# #常量
# SCHOOL = '合肥学院'
# SCHOOL = '低调'
# print(SCHOOL)
'''
用户与程序交互:
输入:
input()
输出:
print()
'''
# # 让用户输入用户名
# name = input('请输入名字:')
#
# # 输出用户名
# print(name)
#
# # 在Python3中,input内输入的任何数据类型都是字符串
# print(type(name))
'''
注释:
单行:
#
多行:
"""
'''
# 单行注释
'''
多行注释
'''
print('hello world!')
'''
字符串格式化输出
'''
# 把100替换给了%s
# str1 = '尊敬的用户,你好!您本月的话费扣除%s元,还剩0元。' % 100
# 把一百替换给了%s,把50替换给了%d
# str1 = '尊敬的用户,你好!您本月的话费扣除%s元,还剩%d元。' % ('一百',50)
# print(str1)
# 报错
# str1 = '尊敬的用户,你好!您本月的话费扣除%s元,还剩%d元。' % ('一百','50')
# print(str1)
'''
优先掌握的操作:
1、按索引取值(正向取+反向取):只能取
2、切片(顾头不顾尾,步长)
3、长度len
4、成员运算in和not in
5、移除空白strip
6、切分split
7、循环
'''
# # 1、按索引取值(正向取+反向取):只能取
# # 正向取
# str1 = 'hello cocoa!'
# print(str1[0]) # h
# print(str1[10]) # a
#
# # 反向取
# print(str1[-2]) # a
# print(str1[-1]) # !
# # 2、切片(顾头不顾尾,步长)
# str1 = 'hello cocoa!'
# # 0 — (5 - 1)
# print(str1[0:5]) # hello
#
# # 步长
# print(str1[0:12]) # hello cocoa!
# print(str1[0:12:2]) # hlocca
# # 3、长度len
# str1 = 'hello cocoa!'
# print(len(str1)) # 12
# # 4、成员运算in和not in
# print('h' in str1) # True
# print('h' not in str1) # False
# # 5、移除空白strip
# # 会移除字符串中左右两边的空格
# str1 = ' hello cocoa!'
# print(str1)
# print(str1.strip())
#
# # 去除制定字符串
# str2 = '!cocoa!'
# print(str2.strip('!'))
# # 6、切分split
# str1 = 'hello cocoa!'
# # 根据str1内的空格进行切分
# # 切分出来的值会存放在[]列表中
# print(str1.split(' ')) # ['hello','cocoa!']
# # 7、循环
# # 对str1字符串进行遍历,打印每一个字符
# str1 = 'hello cocoa!'
# for line in str1:
# print(line)
print('hello world!')
'''
基本数据类型:
数字类型:
1、整型:int
人的年龄、身份证号
2、浮点型:float
人的身高、体重、薪资
'''
# # int
# age1 = int(18)
# print(age1)
# print(type(age1))
#
# age2 = 19
# print(age2)
# print(type(age2))
#
# # float
# sal = 1.01
# print(sal)
# print(type(sal))
'''
字符串类型:
str
作用:
名字、性别、国籍,地址等描述信息
定义:
在单引号\双引号\三引号内,由一串字符组成。
优先掌握的操作:
1、按索引取值(正向取+反向取):只能取
2、切片(顾头不顾尾,步长)
3、长度len
4、成员运算in和not in
5、移除空白strip
6、切分split
7、循环
'''
# # 双引号
# str2 = '今晚月光真好'
# print(str2)
# print(type(str2))
#
# # 三引号
# str3 = '''
# 安徽省
# 合肥市
# 最低调的
# 合肥学院
# '''
# print(str3)
# print(type(str3))
'''
字符串类型:
需要掌握的
'''
# #1、strip,lstrip,rstrip
# str1 = ' hello cocoa '
# print(str1)
# # 去掉两边空格
# print(str1.strip())
# # 去掉左边空格
# print(str1.lstrip())
# # 去掉右边空格
# print(str1.rstrip())
# # 2、lower,upper
# str1 = 'hello cocoa'
# # 转化成小写
# print(str1.lower())
# # 转化成大写
# print(str1.upper())
# # 3、startswitch,endswitch
# str1 = 'hello cocoa'
# # 判断str1字符开头是否等于hello
# print(str1.startswith('hello')) # True
# # 判断str1字符开头是否等于cocoa
# print(str1.endswith('cocoa')) # True
# # 4、format(格式化输出)的三种玩法
# # str1 = 'my name is %s,my age is %s!' % ('cocoa',17)
# # print(str1)
#
# # 方式一:根据位置顺序格式化
# print('my name is {},my age is {}!'.format('cocoa',17))
#
# # 方式二:根据索引格式化
# print('my name is {0},my age is {1}!'.format('cocoa',17))
#
# # 方式一:指名道姓地格式化
# print('my name is {name},my age is {age}!'.format(age=17,name='cocoa'))
# # 6、join字符串拼接
# # 报错,只允许字符串拼接
# # print(' '.join(['cocoa',17]))
# # 根据空格,把列表中的每一个字符串进行拼接
# print(' '.join(['cocoa','17','from China'])) # cocoa 17 from China
# # 根据_,把列表中的每一个字符串进行拼接
# print('_'.join(['cocoa','17','from China'])) # cocoa_17_from China
# # 7、replace:字符串替换
# str1 = 'my name is cocoa,my age is 17!'
# print(str1)
# str2 = str1.replace('cocoa','cute')
# print(str2)
# # 8、isdigit:判断字符串是否是数字
# choice = input('请选择功能[0,1,2]:')
# # 判断用户输入的选择是否有数字
# print(choice.isdigit())
2.功能实现
name = " aleX"
print(name)
print(name.strip())
print(name.startswith('al'))
print(name.endswith('X'))
name1 = name.replace(' aleX',' apeX')
print(name1)
print(name.split('l'))
print(name.upper())
print(name.lower())
print(name[1])
print(name[0:2])
print(name[-2:])
print(name.index('e'))
name2 = 'oldboy'
print(name2.strip('y'))
来源:https://www.cnblogs.com/cocoa-dk/p/11080208.html