python打卡练习4.1

这篇博客主要介绍了Python中的元组和字符串基础知识,包括元组的使用、字符串的转义、三引号、切片与拼接、内置函数如count、strip等,并详细解析了相关内置函数的参数和用法,最后提供了习题练习及解答。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思维导图

元组

元组

字符串

字符串

元组用法

字符串用法

用""进行转义

print('let\'s go') # let's go
print("let's go") # let's go
print('C:\\now') # C: \now
print("C:\\Program Files\\Intel\\Wifi\\Help") # C:\Program Files\Intel\Wifi\Help

  • 常用转义字符
转义字符描述
\反斜杠符号
单引号
"双引号
\n换行
\t横向制表符(TAB)
\r回车

三引号运用

para_str ="""
这是一行多行字符,
可包括制表符(\t),
和换行[\n],

"""
print(para_str)

这是一行多行字符,
可包括制表符(	),
和换行[
],

切片与拼接

str1 = 'I Love LsgoGroup
print(str1[:6]) # I Love
print(str1[5]) # e
print(str1[:6] +”插入的字符串" + str1[6:])
# I Love插入的字符串 L sgoGroup
s ='Python'
print(s) # Python 
print(s[2:4]) # th
print(s[-5:-2])
# yth
print(s[2]) # t
print(s[-1])
#n

内置函数

book ="book"
Book =book.capitalize()
print(Book)
book2=Book.lower()
print(book2)
book3 =book2.upper()
print(book3)
book4 =book3.swapcase()
print(book4)
# Book
# book
# BOOK
# book

count

  • count(str,beg=0,end =len(string))
string ="DAXIExiaohai"
print(string.count('xi'))
# 1--区分大小写,可以指定范围

just

>>> str ="0101"
>>> str.ljust(8,'0')
'01010000'
>>> str2 ='1010'
>>> str2.rjust(8,"0")
'00001010'

strip

>>> chars =" Make in China "
>>> chars2 =chars.lstrip()
>>> chars3 =chars.rstrip()
>>> char5 =chars.strip()
>>> print(chars2,'\n',chars3,'\n',char5)
Make in China  
  Make in China 
 Make in China

partition

>>> str5 ="I love China"
>>> str5 =" I love China "
>>> str5.strip().partition("C")
('I love ', 'C', 'hina')
>>> str5.strip().rpartition("h")
('I love C', 'h', 'ina')
>>> str =" I love Beijing "
>>> str.strip().replace("I","We")
'We love Beijing'

split(str=’ ',num)

>>> string = "  Changjiang River is the most beautiful scence in the world! "
>>> string2 =string.strip().split()
>>> string3 =string.strip().split("o")
>>> print(string2,'\n',string3)
['Changjiang', 'River', 'is', 'the', 'most', 'beautiful', 'scence', 'in', 'the', 'world!'] 
 ['Changjiang River is the m', 'st beautiful scence in the w', 'rld!']

str3 ="""
 say
hello
world 
"""
str3.split('\n')
# ['', ' say', 'hello', 'world ', '']

string = "hello boy< [www. baidu. com] >byebye"
print(string.split('[')[1].split(']')[0]) # www. baidu. com
print(string.split('[')[1].split('l')[0].split('.')) # ['www', 'baidu', 'com']

splitlines([keepends])

>>> str ="I live\nin\nChina"
>>> str.splitlines()
['I live', 'in', 'China']

maketrans 和 translate

str ="This is string example...wow"
intab ='aeiou'
outlab ='12345'
>>> translab =str.maketrans(intab,outlab)
>>> print(translab)
{97: 49, 101: 50, 105: 51, 111: 52, 117: 53}
>>> str.translate(translab)
'Th3s 3s str3ng 2x1mpl2...w4w'

格式符

符号描述
%c格式化字符及其ASCI码
%s格式化字符串,用sr0方法处理对象
%r格式化字符串,用per0方法处理对象
%d格式化整数
%o格式化无符号八进制数
%x格式化无符号十六进制数
%X格式化无符号十六进制数(大写)
%f格式化浮点数字,可指定小数点后的精度
%e用科学计数法格式化浮点数
%E作用同%e,用科学计数法格式化浮点数
%g根据值的大小决定使用%f或%e
%G作用同%g,根据值的大小决定使用%f域%E

辅助命令
符号功能

mnm 是显示的最小总宽度,n是小数点后的位数(如果可用的话)
-用做左对齐
+在正数前面显示加号(+)
#在八进制数前面显示零(0),在十六进制前面显示’0x’或者’0X(取决于用的是x还是X)
0显示的数字前面填充’0而不是默认的空格

习题练习及解答

1、
> 元组概念
写出下面代码的执行结果和最终结果的类型
* (1, 2)*2
* (1, )*2
* (1)*2

```python
answer1 =(1,2) * 2
answer2 =(1,) *2
answer3 =(1) *2
print(answer1)
print(answer2)
print(answer3)

# (1, 2, 1, 2)
# (1, 1)
# 2
"""
* 是重复操作符,分别对元组,元组中的数,和数进行重复
"""
```
2
>2、拆包过程是什么?
a, b=1, 2
上述过程属于拆包吗?
可迭代对象拆包时,怎么赋值给占位符?



```
# 答案可参考:https://blog.youkuaiyun.com/weixin_42635759/article/details/81990343?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param

拆包过程是指利用一句赋值语句按顺序将元组中的各元素赋值给=左边的各个变量。
上述过程属于拆包,1, 2已经定义了一个元组,与左边结构相对应,可以算作解压元组。
在可迭代对象拆包时,使用_(单个元素),*_(连续多个元素)进行占位。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值