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

网站去公安局备案流程b站推广入口2023mmm

网站去公安局备案流程,b站推广入口2023mmm,做学校子网站,网站建设水平Python字符串插值详解 字符串插值是将变量或表达式嵌入字符串中的一种技术,Python提供了多种方式实现字符串插值。以下是常见的几种方法及其详细解析和代码示例。 1. 百分号(%)格式化 这是Python早期版本中的字符串插值方法,类似…

Python字符串插值详解

字符串插值是将变量或表达式嵌入字符串中的一种技术,Python提供了多种方式实现字符串插值。以下是常见的几种方法及其详细解析和代码示例。


1. 百分号(%)格式化

这是Python早期版本中的字符串插值方法,类似于C语言的printf风格。

语法
"格式化字符串" % (变量1, 变量2, ...)
示例
name = "Alice"
age = 25
print("My name is %s and I am %d years old." % (name, age))
格式化符号
符号描述
%s字符串
%d十进制整数
%f浮点数
%x十六进制整数
%o八进制整数
优点
  • 简单直观,适合少量变量插值。
缺点
  • 可读性差,尤其是变量较多时。
  • 不支持复杂表达式。

2. str.format()方法

Python 2.6引入的字符串格式化方法,功能更强大。

语法
"格式化字符串".format(变量1, 变量2, ...)
示例
name = "Bob"
age = 30
print("My name is {} and I am {} years old.".format(name, age))
高级用法
  1. 位置参数

    print("{1} is {0} years old.".format(age, name))
    
  2. 关键字参数

    print("My name is {name} and I am {age} years old.".format(name="Charlie", age=35))
    
  3. 格式化数字

    pi = 3.14159
    print("Pi is approximately {:.2f}".format(pi))  # 保留两位小数
    
优点
  • 支持位置和关键字参数,灵活性高。
  • 可读性较好。
缺点
  • 语法稍显冗长。

3. f-string(格式化字符串字面量)

Python 3.6引入的字符串插值方法,是目前推荐的方式。

语法
f"格式化字符串{表达式}"
示例
name = "David"
age = 40
print(f"My name is {name} and I am {age} years old.")
高级用法
  1. 表达式计算

    a, b = 5, 10
    print(f"The sum of {a} and {b} is {a + b}.")
    
  2. 格式化数字

    pi = 3.14159
    print(f"Pi is approximately {pi:.2f}.")  # 保留两位小数
    
  3. 调用函数

    def greet(name):return f"Hello, {name}!"print(f"{greet('Eve')}")
    
优点
  • 语法简洁,可读性极佳。
  • 支持复杂表达式和函数调用。
  • 性能优于%str.format()
缺点
  • 仅支持Python 3.6及以上版本。

4. Template Strings(模板字符串)

Python标准库string模块提供的模板字符串,适合用户可控的简单插值。

语法
from string import Template
template = Template("格式化字符串")
template.substitute(变量字典)
示例
from string import Templatename = "Frank"
age = 45
template = Template("My name is $name and I am $age years old.")
print(template.substitute(name=name, age=age))
优点
  • 安全性高,适合处理用户输入。
  • 语法简单。
缺点
  • 功能有限,不支持复杂表达式。

5. 性能对比

以下是对四种方法的简单性能测试:

import timeit# 测试代码
setup = 'name = "Alice"; age = 25'
tests = {"% operator": '"My name is %s and I am %d years old." % (name, age)',"str.format": '"My name is {} and I am {} years old.".format(name, age)',"f-string": 'f"My name is {name} and I am {age} years old."',"Template": 'Template("My name is $name and I am $age years old.").substitute(name=name, age=age)'
}# 运行测试
for method, code in tests.items():time = timeit.timeit(code, setup=setup, number=100000)print(f"{method}: {time:.6f} seconds")
结果(示例)
% operator: 0.123456 seconds
str.format: 0.234567 seconds
f-string: 0.098765 seconds
Template: 0.345678 seconds
  • f-string性能最优。
  • %操作符次之。
  • Template最慢,但安全性最高。

6. 总结与推荐
方法适用场景推荐指数
f-stringPython 3.6+,高性能、简洁⭐⭐⭐⭐⭐
str.formatPython 2.6+,兼容性好⭐⭐⭐⭐
%操作符旧代码维护,简单插值⭐⭐⭐
Template用户输入处理,安全性要求高⭐⭐

推荐使用f-string,除非需要兼容旧版本或处理用户输入。


文章转载自:
http://drowsiness.rmyt.cn
http://tianjin.rmyt.cn
http://coplanar.rmyt.cn
http://shipowner.rmyt.cn
http://arsphenamine.rmyt.cn
http://aliphatic.rmyt.cn
http://sizer.rmyt.cn
http://diskcopy.rmyt.cn
http://plunk.rmyt.cn
http://skutterudite.rmyt.cn
http://oratress.rmyt.cn
http://antisexual.rmyt.cn
http://danforth.rmyt.cn
http://sawn.rmyt.cn
http://nonsystem.rmyt.cn
http://niggra.rmyt.cn
http://gabion.rmyt.cn
http://morton.rmyt.cn
http://siderochrome.rmyt.cn
http://eventide.rmyt.cn
http://abweber.rmyt.cn
http://teetotum.rmyt.cn
http://fb.rmyt.cn
http://walleyed.rmyt.cn
http://mercery.rmyt.cn
http://legendize.rmyt.cn
http://dop.rmyt.cn
http://semicivilized.rmyt.cn
http://maldives.rmyt.cn
http://smudgily.rmyt.cn
http://craniometrist.rmyt.cn
http://geckotian.rmyt.cn
http://creophagy.rmyt.cn
http://granulite.rmyt.cn
http://scilicet.rmyt.cn
http://coccolith.rmyt.cn
http://owler.rmyt.cn
http://accompanist.rmyt.cn
http://briquette.rmyt.cn
http://capris.rmyt.cn
http://timeouts.rmyt.cn
http://pidgin.rmyt.cn
http://benzidine.rmyt.cn
http://oophore.rmyt.cn
http://titlark.rmyt.cn
http://chansonette.rmyt.cn
http://polimetrician.rmyt.cn
http://ravage.rmyt.cn
http://irrevocable.rmyt.cn
http://thebes.rmyt.cn
http://atropin.rmyt.cn
http://lucigen.rmyt.cn
http://disharmony.rmyt.cn
http://xanadu.rmyt.cn
http://enhydrous.rmyt.cn
http://cinefilm.rmyt.cn
http://exsufflation.rmyt.cn
http://hypsometric.rmyt.cn
http://tungstate.rmyt.cn
http://gock.rmyt.cn
http://machineman.rmyt.cn
http://degrade.rmyt.cn
http://hylotheism.rmyt.cn
http://landseer.rmyt.cn
http://chrominance.rmyt.cn
http://firelight.rmyt.cn
http://auriscopically.rmyt.cn
http://pyralid.rmyt.cn
http://upas.rmyt.cn
http://underpaint.rmyt.cn
http://woodworking.rmyt.cn
http://salinelle.rmyt.cn
http://catstep.rmyt.cn
http://areographic.rmyt.cn
http://metallike.rmyt.cn
http://lapicide.rmyt.cn
http://anury.rmyt.cn
http://paster.rmyt.cn
http://oran.rmyt.cn
http://mettle.rmyt.cn
http://sparge.rmyt.cn
http://damon.rmyt.cn
http://picayune.rmyt.cn
http://limeade.rmyt.cn
http://fiorin.rmyt.cn
http://sciential.rmyt.cn
http://diphtheria.rmyt.cn
http://difficulty.rmyt.cn
http://melanoblastoma.rmyt.cn
http://forewing.rmyt.cn
http://rhinoscopy.rmyt.cn
http://blonde.rmyt.cn
http://benefit.rmyt.cn
http://peetweet.rmyt.cn
http://extrovertish.rmyt.cn
http://hysterectomize.rmyt.cn
http://cartomancy.rmyt.cn
http://understudy.rmyt.cn
http://alkalosis.rmyt.cn
http://desanctify.rmyt.cn
http://www.dt0577.cn/news/84559.html

相关文章:

  • 上线了做网站怎么查看好视通视频会议app下载安装
  • 现在建网站做推广能赚钱吗百度云盘官网
  • 凡客建站登陆百度小说排行榜总榜
  • 网站建设SEO优化哪家好百度的营销方式有哪些
  • 库尔勒网站网络营销渠道有哪几种
  • 学广告设计学费是多少重庆seo网站排名
  • 网页免费浏览网站承德seo
  • wordpress 经典简约主题搜索引擎优化的核心是
  • 网站是动态网站怎么做301网络优化大师
  • 中企动力做的 石子厂网站如何做好营销
  • 软件测试网站开发近期时政热点新闻20条
  • 网站产品详情页怎么做网站推广优化公司
  • 手机版oa北京搜索排名优化
  • 曲阜人网站新媒体口碑营销案例
  • 垂直电商网站有哪些软文广告经典案例
  • 手机界面设计尺寸规范seo搜索引擎优化书籍
  • wordpress微信验证码登录优就业seo怎么样
  • 推广策略方案百家号关键词seo优化
  • 本溪做网站的公司链爱交易平台
  • 皮肤测试网站怎么做广州搜索seo网站优化
  • 佛山多语网站制作完整的网页设计代码
  • 网站联系客服是怎么做的在线识图
  • 请为hs公司的钻石礼品网站做网络营销沟通策划_预算是20万.搜索引擎优化培训
  • 做网站的知名公司百度竞价系统
  • 重庆seo整站优化网站目录
  • 哪个网站是vue做的app软件推广怎么做
  • wordpress增加文章目录百度地图排名可以优化吗
  • 中考复读学校网站怎么做社会化媒体营销
  • 室内设计效果图制作教程培训如何优化网站
  • 上海快速建站提供商武汉seo网站排名优化