python-3

1.字符串

# str
# str1='helloworldQQQ'
# 大写
# print(str1.upper())  #upper()  全部转为大写

#小写
# str.lower()    #全部转为小写
# print(str1.lower())

# str1.capitalize()   字符串开头转为大写
# str1='helloworldQQQ'
# print(str1.capitalize())

# str1.isupper()   判断字符串是都全大写,返回bool值
# print(str1.isupper())

# str1.islower()   判断字符串是都全小写,返回bool值
# print(str1.islower())

# str2='\n\t'
# str2.isspace()  检测这份字符串是都有空白字符组成
# print(str2.isspace())

# str3='1234a6'
# str3.isdigit()   判断字符串是都由纯数字组成
# print(str3.isdigit())

# str4='12345'
# str4.isalpha()   #判断字符是否由英文组成   A-Z  a -z
# print(str4.isalpha())


# str4.isalnum()   判断字符串是否是英文字母或数字组成
# str3 = 'aaa'
# print(str3.isalnum())

# str1.startswith() 判断字符串是否以指定的子串开头
# str1='helloworldQQQ'
# print(str1.startswith('h') )

# str1.endswith()  判断字符串是都以指定的子串结尾
# str1='helloworldQQQ'
# print(str1.endswith('Q'))

# str1.index()  返回子串在字符串中的索引位
# str1='helloworldQQQ'
# print(str1.index('o'))     #找不到会报错

# str1.count()   返回指定子串在字符串中出现的次数
# str1='helloworldQQQ'
# print(str1.count('h'))


# str1.find()  返回子串在字符串中的索引位
# str1='helloworldQQQ'   #默认只会找第一个
# print(str1.find('Q'))
# print(str1.find('ok'))  找不到返回-1


# str1.rfind()   从右往左找
# str1='helloworldQQQ'
# print(str1.rfind('Q'))   返回正确索引值


# str1.split()   切割字符串,如果什么都不填默认用空格来切割,返回是列表
# str1='helloworld,QQQ'
# print(str1.split())
# print(str1.split(','))


# str1.strip()     默认去除字符串两边的空白符    rstrip() 去掉右边空格    lstrip()  去掉左边的空白格
# str1='   hello   ,world,QQQ   '
# print(str1.strip())

# str1.encode   把字符串编码成字节
# print(str1.encode)


# str1.join()   以当前字符串为分隔符,可将迭代对象中的字符串拼接起来,并且可以设置间隔的形式
# print(','.join(['hello','java','python']))

# str.replace()
# print(str.replace('python is java 2'))  不写默认替换一个  后面加次数是替换几个数  总左到右

print(float('-inf'))   负无穷
print(float('inf'))    正无穷zuoye.py


基础练习
# 1.将字符串 "abcd" 转成大写
str="abcd"
print(str.upper())
# # 2.计算字符串 "cd" 在 字符串 "abcd"中出现的位置
str="abcd"
print(str.index('cd'))

# # 3.字符串 "a,b,c,d" 请用逗号分割字符串,分割后的结果是什么类型的?
str="abcd"
print(type(str.split(',')))

# 4."this is a book\n" 字符串的末尾有一个回车符(换行符),请将其删除
a="this is a book\n"
print(a.rstrip())
5."Python is good" 请将字符串里的Python替换成 java ,并输出替换后的结果



6."irc_nj_class2.py.xxx" 请写程序从这个字符串里获得.py前面的部分(用至少两种方式)
ha="irc_nj_class2.py.xxx"
a=ha.split('.')
print(a[0])

print(ha[:-7])

print("irc_nj_class2.py.xxx".replace('.py.xxx',''))

7."this is a book" 请用程序取出该英文句子的第一个单词并打印 (用至少两种方式
ha="this is a book"
a=ha.split(' ')
print(a[0])

print(ha[:5])


# 8."this is a book" 请用程序判断该字符串是否以apple结尾
ha="this is a book"
print(ha.endswith('apple'))   #False


9."This IS a book" 请将字符串里的所有字符转成小写字符
ha="This IS a book"
print(ha.lower())

10.请用代码实现:利用下划线将列表的每一个元素拼接成字符串,li=['周润发','周杰伦', '周星驰']
print('_'.join(['周润发','周杰伦', '周星驰']))


 元组
# tuple   元组
#
# tuple 元组和列表非常相似,唯一的区别不可变

# 元组的初始化
# tup=(1,2,3)   由小括号和逗号组成
# print(tup)
# print(type(tup))

# a=(1,)  #这是元组,当你的元祖中只有一个元素,也需要加逗号
# print(a,type(a))


#初始化一个空元组
# 1.
# tup=()
# print(type(tup))

# 2.
# tup=tuple()
# print(tup,type(tup))


#字符串转元组
# t=tuple('fwefg')      ('f', 'w', 'e', 'f', 'g')
# print(t)


#列表转元组
# lst=[1,2,3,4]
# t=tuple(lst)   (1, 2, 3, 4)
# print(t)


#查看
tup=(1,2,3,True,'hello')
# 1.索引
# print(tup[3])

# 2.切片
# print(tup[2:4])  切出来还是元组


# 3.tuple,index()  根据元素查找索引
# print(tup.index(1))

# 4.tuple.count()   统计有多少相同的元素
# print(tup.count(2))


# 改--另类的改变元组   只能在元组的子列表下进行添加修改
# tup=(1,2,3,4,True,'HFUI',['FEF',123])
# # print(tup[-1])
# 修改
# tup[-1][1]='goud'
# print(tup)
# 添加
# tup[-1].append('gouzei')
# print(tup)
#
#
# 元组其他的操作
# 1.元组和元组相加 + 拼接
# print((1,2)+(3,4))
#
# 2.元组和整型相乘
# print((1,2)*2)
#
# 3.元组的成员运算
# print(1 in (1,2,3))
# print(4 not in (1,2,3))
#
# 4.元组的长度
# print(len(tup))


#元组的打包和拆包
# a=1,2,3,4
# print(a)
# print(type(a))
#
# 拆包
# tup1=(1,2,3,4)
# a,b,c,d=tup1
# print(a,b,c,d)   # 1 2 3 4


#列表也可以拆包
# lst=[543,56]
# a,b=lst
# print(a,b)    543 56


#变量的交换
# num1=100
# num2=200
# num1,num2=num2,num1
# print(num1,num2)     #200 100




可变类型和不可变类型
3.可变类型和不可变类型
# 列表、字典、集合---可变类型(内存地址可变)      字符串、元组、整型、浮点、bool值、set表----不可变类型 (内存地址不变)

深浅拷贝

1mport copy    2.切片
#浅拷贝:我并不是在内存生成完全隔离的数据,只有第一-层隔离J数据, 1.copy    2.切片
#如果非第一层的内存地址数据发生了变化,我所有拷贝的列表都共享这一-变化
L = ['易大师','赵信','剑圣',['德玛']]
11 = t[:]
13 = l. copy()
# print(l)
# print(I1)
# print(12)
# l1.append( 'hello worZd')
# print(l1)
# print(l)
# print(l2)
# l1[3].append( '盖伦')
# print(l1)
# print(l)
# print(L2)
#深拷贝
#全部数据/结构都进行了复制,连内存地址都拷贝了,(浪费内存空间)
12 = copy . deepcopy(l)
12[3] .append('及时雨,宋江')
print(1)
print(11)
print(L3)
print(12)


l[-1].append('红太狼')
print(l)
print(l1)
print(l2)



深拷贝
全部的数据和结构都进行了复制,连内存地址都拷贝了,浪费内存空间
l2=copy.deepcopy(l)

基础练习
 作业
# 1.写一个程序,接收用户输入任意内容,然后判断该内容中的英文、数字、空白符、其它字符分别有多少
# content = input('请输入:')
# alpha_count = num_count = space_count = other_count = 0
# index = 0
# while index < len(content):
#     if content[index].isalpha():
#         alpha_count += 1
#     elif content[index].isdigit():
#         num_count += 1
#     elif content[index].isspace():
#         space_count += 1
#     else:
#         other_count += 1
#     index += 1
# print('英文{},数字{},空白{},其他字符{}'.format(alpha_count,num_count,space_count,other_count))
# 2.输入用户名,判断用户名是否合法(用户名必须包含且只能包含数字和字母,并且第一个字符必须是大写字母)
# content = input('请输入:')
# if content.isalnum() and content[0].isupper():
#     print('合法')
# else:
#     print('不合法')


# 3.输入一个字符串,将字符串中所有的数字字符取出来产生一个新的字符串
# content = input('请输入:')
# index = 0
# new_str = ''
# while index < len(content):
#     if content[index].isdigit():
#         new_str +=content[index]
#     index += 1
# print(new_str)
# 4.给定一个英文句子比如"hello python" 将句子中的单词位置反转为 "python hello"
# s = 'hello python'
# lst=s.split()
# lst.reverse()
# print(' '.join(lst))

# print(' '.join(s.split()[::-1]))


# 5.假设有任意字符串如:"hello world",写程序把字符串中所有l出现的索引位加入到一个列表,如[2, 3, 9]
# str_1 = 'hello world'
# lst = []
# index = 0
# while index< len(str_1):
#     if str_1[index] == 'l':
#         lst.append(index)
#     index += 1
# print(lst)


# 6.有一个url:https://iruance.com?username=admin&password=123456&sex=GG&from=China,
# 将url中所有参数值取出并装在一个列表中,如['admin', '123456', 'GG', 'China']
# url = 'https://iruance.com?username=admin&password=123456&sex=GG&from=China,'
# params = url.split('?')[1]
# params_list=params.split('&')
# print(params_list)
# index = 0
# new_lst = []
# while index < len(params_list):
#     # params_list[index]=params_list[index].split('=')[1]
#     new_lst.append(params_list[index].split('=')[1])
#     index += 1
# print(new_lst)

# 7.请使用三种方式反转字符串  str01 = "hello"
# str01 = "hello"
# print(str01[::-1])

# str01 = "hello"
# lst=list(str01)
# lst.reverse()
# print(''.join(lst))

# str01 = "hello"
# new_str = ''
# index = len(str01) - 1
# while index >= 0:
#     new_str += str01[index]
#     index -= 1
# print(new_str)

# 8.有一个英文句子,比如 'hello world hello python',将其中的每一个单词首字母大写
# str2 = 'hello world hello python'
# # 1.把各个单词拆分开
# lst1 = str2.split()
# # print(lst1)
# index = 0
# while index < len(lst1):
#     lst1[index] = lst1[index].capitalize()
#     index += 1
#
# print(' '.join(lst1))

# 9.# 有一个字符串HellO WorlD,将字符串中所有的小写字母转大写,大写字母转小写

str3 = 'HellO WorlD'

index = 0
new_str = ''
while index<len(str3):
    if str3[index].isupper():
        new_str += str3[index].lower()
    else:
        new_str += str3[index].upper()
    index += 1
print(new_str)


                
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值