码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • python--字符串格式化和列表


    python--字符串格式化和列表

    • 作业
    • 字符串格式化
    • format()
      • 按顺序取值
      • 按索引取值(0开始)
      • 按关键字取值
      • 调整精度
      • 百分比格式化
    • 列表
      • 列表的特点
      • 列表的创建
      • 列表的增
      • 列表的删
        • 3.1 list.pop(index)
        • 3.2 list_1.remove(2)
        • 3.3 clear() 清空列表
        • 3.4 del list_1[index]
      • 列表的改
        • 4.1修改元素的值
        • 4.2插队元素insert
        • 4.3合并元素extend
      • 列表的查
        • 5.1 通过索引获取元素
        • 5.2 获取元素的索引
        • 5.3 获取列表长度
        • 5.4 切片

    作业

    3、用python实现"hello world hello python" 变成 python hello world hello

    str_1 = "hello world  hello python"
    # 使用字符串的 split() 方法将字符串分割成单词列表(空格)
    new_str = str_1.split()
    #print(new_str)
    # 使用字符串的 join() 方法将反转后的单词列表连接成一个字符串
    res = " ".join(new_str[::-1])
    # 打印结果
    print(res)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    字符串格式化

    1、%s
    2、%d int(正数、负数) 、float(会抹掉小数部分)
    3、%f price = “the price is %.2f” %(9.9)

    # %d
    price = "the price is %d" %(9.9)
    print(price)
    
    • 1
    • 2
    • 3
    # %f(保留2位置)
    price = "the price is %.2f" %(9.9)
    print(price)
    
    • 1
    • 2
    • 3

    format()

    按顺序取值

    price = “the price is {} {} {}”.format(10.99,100,200)

    # 按顺序取值
    price = "the price is {}  {}  {}".format(10.99,100,200)
    print(price)
    
    • 1
    • 2
    • 3

    按索引取值(0开始)

    price =“the price is {0} {1} {1}”.format(100,200)

    # 按索引取值(0开始)
    price ="the price is {0}  {1}  {1}".format(100,200,100)
    print(price)
    
    • 1
    • 2
    • 3

    按关键字取值

    price =“the price is {price1} {price1} {price2}”.format(price2=200,price1=100)

    #按关键字取值
    price ="the price is {price1}  {price1}  {price2}".format(price2=200,price1=100)
    print(price)
    
    • 1
    • 2
    • 3

    调整精度

    price =“the price is {:.2f}”.format(12.5364675)

    #调整精度
    # price = "the price is %.2f" %(9.9)
    price ="the price is {:.2f}".format(12.5364675)
    print(price)
    
    • 1
    • 2
    • 3
    • 4

    百分比格式化

    price =“the price is {:.2%}”.format(0.5364675)

    # 百分比格式化
    price ="the price is {:.2%}".format(0.5364675)
    print(price)
    
    • 1
    • 2
    • 3

    列表

    列表的特点

    1、索引从0开始
    2、list是有序的
    3、可变的
    4、元素可以重复
    5、同一个listr支持任意数据类型

    列表的创建

    list_1=[1,2,3,4,5]

    列表的增

    list_1=[1,2,3,4,5]

    列表的删

    3.1 list.pop(index)

    index=None:删除最后一个元素
    index=索引值:删除对应索引的元素
    正确删除了返回被删除的元素

    # pop
    list_1=[1,2,3,4,5,'adfadf','bdfd']
    result=list_1.pop(0)
    print(result)
    
    • 1
    • 2
    • 3
    • 4

    3.2 list_1.remove(2)

    删除匹配到的第一个元素
    如果说元素匹配失败,直接报错
    正确删除返回None

    # remove 移除/删除
    list_1=[1,2,3,4,5]
    result=list_1.remove(2)
    print(result)
    
    • 1
    • 2
    • 3
    • 4

    3.3 clear() 清空列表

    # 删除
    list_1=[1,2,3,4,5,6]
    # clear
    result=list_1.clear()
    print(list_1)
    print(result)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3.4 del list_1[index]

    按索引删除,没返回值

    列表的改

    4.1修改元素的值

    list_1[index]=value
    
    • 1

    4.2插队元素insert

    list_1=[1,2,3,4,5,'adfadf','bdfd']
    list_1.insert(1,'test_insert')
    print(list_1)
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    4.3合并元素extend

    list_1=[1,2,3,4,5,'adfadf','bdfd']
    list_2=['a','b','c']
    list_1.extend(list_2)
    print('这里是list_1:',list_1)
    print('这里是list_2',list_2)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    列表的查

    5.1 通过索引获取元素

    list_1[index]
    
    • 1

    5.2 获取元素的索引

    list_1.index(value)
    
    • 1

    5.3 获取列表长度

     len(list_1)
    
    • 1

    5.4 切片

    list[开始索引:结束索引:步长]
    
    • 1
  • 相关阅读:
    Redis的hash数据类型——Redis
    【数学建模】高速车辆流体-结构-射流相互作用分析和建模附matlab代码
    2.9 PE结构:重建导入表结构
    【Pandas数据处理100例】(九十九):Pandas使用at_time()筛选出特定时间点的数据行
    java计算机毕业设计在线毕设选题系统源码+系统+mysql数据库+lw文档
    记一次内网靶机实战
    oracle常用命令
    临沂大学张继群-智慧农业项目招募
    7-38 掉入陷阱的数字
    《nlp入门+实战:第八章:使用Pytorch实现手写数字识别》
  • 原文地址:https://blog.csdn.net/JennyXi2001/article/details/136731544
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号