python 基础知识点(蓝桥杯python科目个人复习计划6)

今日复习内容:字符串 / 列表 / 列表推导式

一.一些基础知识点

1. python 数据类型

  •  Number (数字)-------数值类型:int , float , bool , complet(复数)
  • String(字符串)
  • List(列表)
  • Tuple(元组)
  • Set (集合)
  • Dicttionary(字典)

2.不可变数据 

   Number(数字)、String(字符串)、Tuple(元组)

3.可变数据

   List(列表)、Dictionary(字典)、Set(集合)

所谓序列,指的是一块可存放多个值的连续内存空间,这些值按一定顺序排列,可以通过每个值

所在位置的编号(称为索引) 访问它们。

为了更形象的认识序列,可以将它看做是一家旅店,那么店中的每个房间就如同序列存储数据的一

个个内存空间,每个房间 所特有的房间号就相当于索引值。也就是说,通过房间号(索引)我们

可以找到这家旅店(序列)中的每个房间(内存空 间)。

# 在 Python 中,序列类型包括字符串、列表、元组、集合和字典

# 集合和字典不支持索引、切片、相加和相乘操作

二.字符串输出(字符串格式化)

1.%用法和format用法

  •     %运算符就是用来格式化字符串的,有几个%占位符,后面就跟几个变量或者值,顺序要对应

    好。

    %d 整数

    %f 浮点数

    %s 字符串

    %x 十六进制整数

  •   format () 

        基本语法是通过 {} 和 :来代替以前使用的 % 

        format ()函数可以接受不限个数的参数,且位置可以不按顺序

        相对基本格式化输出采用 ‘%’ 的方法,format()函数功能更强大,该函数把字符串当                    成一个模板,通过传入的参数进行格式化,并且使用 ‘{}’ 作为特殊字符代替 ‘%’

    (1) format 位置匹配

           print  ('{},{}'.format(12, 34))     # 12,34    

           <1>    不带编号,即 “ {} ”       

                     print  ('{},{}'.format(a, b))    # hello world

           <2>   带数字编号,可调换顺序,即“{1}”、“{2}”

                     print  ('{1},{0},{1}'.format(a, b))     # world,hello,world

           <3>  带关键字,即“{a}”、“{tom}”

                    print('{a1}-{b1}'.format(a1=a, b1='world'))     # hello-world

  (2)   格式化 f ' '
name = 'Tom'
age = 20
print(f'姓名是:{name},年龄是:{age}')

         运行结果:

 

(3) 其他用法

st = '{}+{}'.format
print(st('a', 'b'))# a+b

          运行结果:

 

(4) {:.},其中“:”指定代表元素需要的操作,如":.4f"小数点四位(fload), ":3"占3个字符空间等

a = 1.2355
 print('{:.2f}'.format(a))    # 运行结果:1.24
 a = 'abc'
 print('{:s}'.format(a))      # abc

    运行结果:

       

三.字符串常见操作 

1. find

    检测 str 是否包含在 mystr中,如果是返回开始的索引值,否则返回-1

mystr.find(str, start=0, end=len(mystr))
a = 'hello world'
print(a.find('h'))      # 0 返回开始的索引值
print(a.find('h', 1))   # -1 指定从哪个下标开始寻找,不包含返回-1
print(a.find('l', 1))   #2

   运行结果:

 

2.index查找索引

   跟find()方法一样,只不过如果str不在 mystr中会报一个异常. 

a = 'hello world'
mystr.index(str, start=0, end=len(mystr))
print(a.index('h', 1)) # ValueError: substring not found

运行结果:

 

3.count

   返回 str在start和end之间 在 mystr里面出现的次数

mystr.count(str, start=0, end=len(mystr))
a = 'hello world'
print(a.count("l"))   # 3
print(a.count("l", 3))  # 2

运行结果:

 

4.replace替换

   把 mystr 中的 str1 替换成 str2,如果 count 指定,则替换不超过 count 次.

mystr.replace(str1, str2,  mystr.count(str1))
a = 'hello world'
print(a.replace('l', 'a'))     # heaao worad
print(a.replace('l', 'a', 2))  # heaao world   2的话是指定替换的次数

运行结果:

 

5.split分割

   以 str 为分隔符切片 mystr,如果 maxsplit有指定值,则仅分隔 maxsplit 个子字符串

a = 'he,llo wo,rld'
print(a.split(",")) # ['he', 'llo wo', 'rld']
b = a.split(",")
print(type(b)) # <class 'list'>
a = 'hello world he wo'
b = a.split(' ', 2) # 指定分割次数,如果为0的话,就不分隔
print(b) # ['hello', '

运行结果:

 

6.capitalize

   把字符串的第一个字符大写

a = 'he,llo wo,rld'
mystr.capitalize()
print(a.capitalize()) # Hello world

运行结果:

 

7.title

   把字符串的每个单词首字母大写

a = "hello itcast"
print(a.title())

运行结果:

 

8.startswith

   检查字符串是否是以 hello 开头, 是则返回 True,否则返回 False

mystr.startswith(hello)
a = 'hello'
print(a.startswith('h'))   # True

运行结果:

 

9.endswith

   检查字符串是否以obj结束,如果是返回True,否则返回 False.

mystr.endswith(obj)
a = 'hello'
print(a.endswith('e'))    # False

运行结果:

 

10.lower

     转换 mystr 中所有大写字符为小写

mystr.lower()        
a = 'HEllo'
print(a.lower())   # hello

运行结果:

 

11.upper

     转换 mystr 中的小写字母为大写

mystr.upper()    
a = 'HEllo'
print(a.upper())  # HELLO

运行结果:

 

12.join连接

    mystr 中每个字符后面插入str,构造出一个新的字符串

mystr.join(str)
a = 'hello world'
print('#'.join(a))

运行结果:

 

OK,今天就先复习到这里了!不出意外,明天继续。 

 

 

 

 

 

 

 

 

 

 

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值