当前位置: 首页 > news >正文

6.python——字符串

python中用’ '和" "创建字符串

python的子字符串截取用[]取
在这里插入图片描述
在这里插入图片描述
字符串拼接可以直接用+相加。

python三引号允许一个字符串跨多行,其中无需进行转义(所见即所得)。
当你需要一块HTML或者SQL时,这时用字符串组合,特殊字符串转义将会非常的繁琐。

format 格式化函数

>>>"{} {}".format("hello", "world")    # 不设置指定位置,按默认顺序
'hello world'>>> "{0} {1}".format("hello", "world")  # 设置指定位置
'hello world'>>> "{1} {0} {1}".format("hello", "world")  # 设置指定位置
'world hello world'

也可以设置参数

#!/usr/bin/python
# -*- coding: UTF-8 -*-print("网站名:{name}, 地址 {url}".format(name="菜鸟教程", url="www.runoob.com"))# 通过字典设置参数
site = {"name": "菜鸟教程", "url": "www.runoob.com"}
print("网站名:{name}, 地址 {url}".format(**site))# 通过列表索引设置参数
my_list = ['菜鸟教程', 'www.runoob.com']
print("网站名:{0[0]}, 地址 {0[1]}".format(my_list))  # "0" 是必须的

f—string,过去通过%替换变量

>>> name = 'Runoob'
>>> 'Hello %s' % name
'Hello Runoob'

f—string,现在通过f标识字符串,更加简单,不用判断是%s还是%d

>>> name = 'Runoob'
>>> f'Hello {name}'  # 替换变量
'Hello Runoob'
>>> f'{1+2}'         # 使用表达式
'3'>>> w = {'name': 'Runoob', 'url': 'www.runoob.com'}
>>> f'{w["name"]}: {w["url"]}'
'Runoob: www.runoob.com'

在 Python 3.8 的版本中可以使用 = 符号来拼接运算表达式与结果:

>>> x = 1
>>> print(f'{x+1}')   # Python 3.6
2>>> x = 1
>>> print(f'{x+1=}')   # Python 3.8
x+1=2

在Python3中,所有的字符串都是Unicode字符串。

检测 str 是否包含在字符串中,如果指定范围 beg 和 end ,则检查是否包含在指定范围内,如果包含返回开始的索引值,否则返回-1

find(str, beg=0, end=len(string))
index(str, beg=0, end=len(string))#同上,但如果不在会抛异常

返回 str 在 string 里面出现的次数,如果 beg 或者 end 指定则返回指定范围内 str 出现的次数

count(str, beg= 0,end=len(string))

返回字符串长度

len(string)

文章转载自:

http://Hzq9Iyb9.sgfpn.cn
http://3bmfPovE.sgfpn.cn
http://LSFmPGUU.sgfpn.cn
http://OYTmnwEc.sgfpn.cn
http://JXyvhYRw.sgfpn.cn
http://p5TwrgpY.sgfpn.cn
http://PG7nCKC0.sgfpn.cn
http://ja1VkBWm.sgfpn.cn
http://Pz6YqYTf.sgfpn.cn
http://oLD45rxN.sgfpn.cn
http://FLEt6jrK.sgfpn.cn
http://jNPnmhYi.sgfpn.cn
http://cOE0QGYF.sgfpn.cn
http://ujDY2Xzi.sgfpn.cn
http://crpIHuGc.sgfpn.cn
http://1Hl3ne3E.sgfpn.cn
http://KZHhAta9.sgfpn.cn
http://LVVxtyv7.sgfpn.cn
http://ABjSqwVI.sgfpn.cn
http://qFjE0WYV.sgfpn.cn
http://h5qLfgRY.sgfpn.cn
http://ta4kv8yo.sgfpn.cn
http://SUf9fpxJ.sgfpn.cn
http://IGqj5tVi.sgfpn.cn
http://DPzCMCHE.sgfpn.cn
http://OsX7Oj5j.sgfpn.cn
http://09k9a6tF.sgfpn.cn
http://lKCJYW0d.sgfpn.cn
http://dtu5ja6S.sgfpn.cn
http://S417cHod.sgfpn.cn
http://www.dtcms.com/a/372853.html

相关文章:

  • 懒汉式——LazyMan(任务队列应用)
  • Nginx 实战系列(四)—— Nginx反向代理与负载均衡实战指南
  • Nginx 反向代理 + Tomcat 集群:负载均衡配置步骤与核心原理
  • 【Linux】匿名管道和进程池
  • PWA:打造媲美 Native Apps 的 Web 应用体验
  • # 小程序 Web 登录流程完整解析
  • 2025中国AI HR市场深度洞察:趋势、厂商与未来展望
  • 并发编程的守护者:信号量与日志策略模式解析
  • Flink Task线程处理模型:Mailbox
  • ActiveMQ classic ,artemis ,artemis console ,nms clients,cms client详解
  • 【论文阅读】Far3D: Expanding the Horizon for Surround-view 3D Object Detection
  • Three.js使用outlinePass描边后,描边颜色和背景叠加变淡
  • GPT系列--类GPT2源码剖析
  • 反编译分析C#闭包
  • DTO与POJO:核心差异与最佳实践
  • #C语言——刷题攻略:牛客编程入门训练(九):攻克 分支控制(三)、循环控制(一),轻松拿捏!
  • Android 中 自定义 RecyclerView 控件限制显示高度
  • Codesy中的UDP发送信息
  • Hadoop进程:深入理解分布式计算引擎的核心机制
  • SQL Server死锁排查实战指南
  • 自学嵌入式第三十八天:数据库
  • 【开题答辩全过程】以 基于springboot的酒店管理系统设计与实现为例,包含答辩的问题和答案
  • SpringBoot控制层接收参数处理、Logback日志入门和使用
  • Python快速入门专业版(十三):Python变量进阶:全局变量与局部变量(含global关键字用法)
  • 深度学习(二):神经元与神经网络
  • 如何在不同 iOS 设备上测试和上架 uni-app 应用 实战全流程解析
  • iOS 开发全流程实战 基于 uni-app 的 iOS 应用开发、打包、测试与上架流程详解
  • [论文阅读] 人工智能 + 软件工程 | 大模型破局跨平台测试!LLMRR让iOS/安卓/鸿蒙脚本无缝迁移
  • 汇编基础1
  • CSS @scope与12个降低css冲突方法