首页 > 编程语言 >python中常见的字符串格式化方法

python中常见的字符串格式化方法

时间:2023-05-20 17:33:49浏览次数:39  
标签:old name python height 字符串 格式化 years

1. 使用 % 符号进行字符串格式化

使用 % 符号是一种较为传统的字符串格式化方法。它通过将占位符 %s 插入到字符串中,再使用 % 运算符将具体的值插入到这些占位符中。例如:

name = "Alice"
age = 20
height = 175
print("My name is %s, I'm %d years old, and my height is %.2f." % (name, age, height))

输出结果为:My name is Alice, I'm 20 years old, and my height is 175

2. 使用 format() 方法进行字符串格式化

format() 方法是一个更加灵活和推荐的字符串格式化方法。它通过 {} 占位符来表示需要插入具体值的位置,然后使用 format() 方法将这些值按顺序插入到占位符所在的位置。例如:

name = "Bob"
age = 25
height = 180.0
print("My name is {}, I'm {} years old, and my height is {:.1f}.".format(name, age, height))

输出结果为:My name is Bob, I'm 25 years old, and my height is 180.0.

3. 使用 f 字符串进行字符串格式化

从 Python 3.6 开始,还可以使用 f 字符串对表达式求值,并将结果插入到字符串中。即以 f 或 F 开头,将表达式写在 {} 中。例如:

name = "Charlie"
age = 30
height = 185
print(f"My name is {name}, I'm {age} years old, and my height is {height:.2f}.")

输出结果为:My name is Charlie, I'm 30 years old, and my height is 185

标签:old,name,python,height,字符串,格式化,years
From: https://www.cnblogs.com/peijiao/p/17417509.html

相关文章

  • 字符串中的第一个唯一字符
    给定一个字符串 s ,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回-1 。示例1:输入:s="leetcode"输出:0示例2:输入:s="loveleetcode"输出:2示例3:输入:s="aabb"输出:-1来源:力扣(LeetCode)链接:https://leetcode.cn/problems/first-unique-char......
  • Python request请求 解析
    importloggingimporthttp.clienthttpclient_logger=logging.getLogger("http.client")defhttpclient_logging_patch(level=logging.DEBUG):"""EnableHTTPConnectiondebugloggingtotheloggingframework"""......
  • python 检测屏幕指定区域 有变化即截图
    fromPILimportImageChops#$pipinstallpillowfrompyscreenshotimportgrab#$pipinstallpyscreenshotfromdatetimeimportdatetimeimporttime,sys,reim=grab(bbox=(160,180,1760,1080))a=0whileTrue:#http://effbot.org/zone/pil-comparing-imag......
  • 基于python实现-根据Excel表格指定的UniqueKey的顺序-到另一个参考表格中查找-补全与
    今天笔者在整理一份数据时,有这样一个需求,已知有多个ID是UniqueKey,每一个UniqueKey及与它相关的数据为一行,存放于Excel表格行中但他们相关的数据可能有误,而另一个表格Excel-02中的数据没有问题,但是UniqueKey顺序与第一个表格不一样现在主要是要修改第一个表格的数据,当然可以使用......
  • 使用Python实现MACD策略
    importpandasaspdimportnumpyasnpdefcalculate_macd(data,short_period=12,long_period=26,signal_period=9):"""计算MACD指标和信号线参数:data:包含价格数据的DataFrame,需包含'Close'列short_period:快线的计算周期,默认为12long_p......
  • Python selenium
    初始化webdriveropts=webdriver.chrome.options.Options()#无头模式opts.add_argument("--headless")opts.add_argument("--disable-gpu")#驱动地址driver_path=os.path.join(os.path.dirname(__file__),"./driver/chromedriver.exe"......
  • 代码随想录算法训练营第十一天|20. 有效的括号、1047. 删除字符串中的所有相邻重复项
    【参考链接】20.有效的括号【注意】1.括号匹配是使用栈解决的经典问题。2.这个命令最后进入a目录,系统是如何知道进入了a目录呢,这就是栈的应用(其实可以出一道相应的面试题了)。3.有三种不匹配的情况,第一种情况,字符串里左方向的括号多余了;第二种情况,括号没有多余,但是括号的......
  • 用python读取excel文件
    需要用到的包--pandasimportpandasaspd简单读取excel文件,要用到read_excel()df=pd.read_excel("D:\无名字的文件夹\实验材料.xlsx")dfOut[11]:姓名年龄喜好0张三15抖音1李四16快手2王五17抖音3小明18小红书4小花19小红书使用r......
  • python-docx - 1
    python-docx用于创建和更新Word文件的python库1.安装pip3installpython-docx-ihttps://mirrors.aliyun.com/pypi/simple2.创建与保存文件#导入DocumentfromdocximportDocument#创建一个新文档doc=Document()print(type(doc))#<class'docx.document.Doc......
  • 用python处理word文档的操作
    提取超链接fromdocximportDocument注意docx包不是python自带的包需要下载下一篇,我会给大家说相关的下载,可以点这里跳转到下一篇博客fromdocximportRTimportred=Document('D:\无名字的文件夹\python练习\材料.docx')forpind.paragraphs:rels=d.part.relsforrel......