python中的printf:%号拼接字符串和format函数
在C语言中,我们使用printf("%s","hello")这种形式进行字符串的拼接
在python中,进行这样的拼接有两种实现方式,分别是%号拼接以及使用format函数,以下进行代码演示
%号拼接字符串
在python中是用%号可以进行字符串的拼接,这个跟print函数是无关的。以下进行举例
- 打印字符串
msg = "i am %s,my blogs is %s" % ("CodeScrew","www.cnblogs.com/codescrew")
print(msg) - 打印浮点数
msg = "i am %0.2f m" %1.785
print(msg) #打印结果为i am 1.78 m其中%号后面的.2表示保留2位小数
- 打印百分比
msg = "it is %0.2f %%" % 99.852
print(msg) #打印结果为it is 99.85 % - 使用键值对进行拼接
msg = "i am %(name)s.my age is %(age)d" % ({"name":"CodeScrew","age":23})
print(msg) #打印结果为i am CodeScrew.my age is 23
format函数处理字符串
除了%号进行拼接,还可以使用字符串类的format函数,以下列举了常用的使用。
msg = "i am {},age is {}".format("CodeScrew",23)
print(msg) #打印结果为i am CodeScrew,age is 23
msg = "i am {1},age is {0}".format("CodeScrew",23)
print(msg) #打印结果为i am 23,age is CodeScrew
msg = "i am {name},age is {age}".format(name="CodeScrew",age=23)
print(msg) #打印结果为i am CodeScrew,age is 23
msg = "i am {name},age is {age}".format(**{"name":"CodeScrew","age":23})
print(msg) #打印结果为i am CodeScrew,age is 23
msg = "i am {:s},age is {:d}".format("CodeScrew",23)
print(msg) #打印结果为i am CodeScrew,age is 23
msg = "i am {:s},age is {:d}".format(*["CodeScrew",23])
print(msg) #打印结果为i am CodeScrew,age is 23
msg = "Numbers:{:b},{:o},{:d},{:x},{:X}".format(15,15,15,15,15)
print(msg) #打印结果为Numbers:1111,17,15,f,F
python中的printf:%号拼接字符串和format函数的更多相关文章
- 为什么 Java 8 中不再需要 StringBuilder 拼接字符串
为什么 Java 8 中不再需要 StringBuilder 拼接字符串 来源:codeceo 发布时间:2016-12-27 阅读次数:427 0 在Java开发者中,字符串的拼接占用资源高往往 ...
- Python中执行变量而非字符串
Python中执行变量而非字符串 设想这样的场景,你需要大型项目的开发.但是项目的开发第一步是啥? 当然是import导入了. ...but............ 默认 import 后面跟着字符串 ...
- Python第二天 变量 运算符与表达式 input()与raw_input()区别 字符编码 python转义符 字符串格式化 format函数字符串格式化 帮助
Python第二天 变量 运算符与表达式 input()与raw_input()区别 字符编码 python转义符 字符串格式化 format函数字符串格式化 帮助 目录 Pychar ...
- python python中那些双下划线开头的那些函数都是干啥用用的
1.写在前面 今天遇到了__slots__,,所以我就想了解下python中那些双下划线开头的那些函数都是干啥用用的,翻到了下面这篇博客,看着很全面,我只了解其中的一部分,还不敢乱下定义. 其实如果足 ...
- 如何在Python中快速画图——使用Jupyter notebook的魔法函数(magic function)matplotlib inline
如何在Python中快速画图--使用Jupyter notebook的魔法函数(magic function)matplotlib inline 先展示一段相关的代码: #we test the ac ...
- python3字符串格式化format()函数的简单用法
format()函数 """ 测试 format()函数 """ def testFormat(): # format()函数中有几个元素, ...
- python中列表和元组以及字符串的操作
python中列表是非常好用的.不过有一些使用小细节还需要注意一下. tag[32:-4] 从index为32到tag的倒数第4个字符. 如果索引为32的值在倒数第4个字符的右边,那么将输出为空.只要 ...
- (四)Python中的“四大才子”(字符串、列表、字典、集合)
前戏:在python中把数据序列分为可变(mutable)和不可变(immutable)两种 不可变:string.int.float.tuple 特点:相同对象只是占用一个内存地址,不管有多少个变量 ...
- python中列表,数字,字符串函数总结
列表list: arr = [] 1.可以定义空列表 2.可以定义只有一个元素的列表 3.元素可以是任意类型 arr.append('abc')末尾添加 arr.insert(index,objec) ...
随机推荐
- 脱upx壳--初试--单步追踪
脱upx壳--初试--单步追踪 这里的练习题目是reversing.kr 的Easy Crack 我自己用upx加壳工具给它加了个壳,由于原文件逻辑简单,所以用它来练练手 之后用到的工具是IDA和Ol ...
- Ajax.html:35 [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org
AJAX的容易错误的地方 Ajax.html:35 [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated ...
- python jquery
jquery 一.寻找元素(选择器和筛选器) a.选择器 1.基本选择器 1 $("*") $("#id") $(".class") ...
- Python/MySQL(一、基础)
Python/MySQL(一.基础) mysql: MYSQL : 是用于管理文件的一个软件 -socket服务端 (先启动) -本地文件操作 -解析 指令[SQL语句] -客户端软件 (各种各样的客 ...
- shell:bash环境
1.什么是shell shell一般代表两个层面的意思,一个是命令解释器,比如BASH,另外一个是shell脚本. 命令解释器shell的发展史,sh-csh-ksh-tcsh-bash. 2.命令的 ...
- else语句的搭配
1.else语句搭配if 要么怎样,要么怎样 2.else语句搭配for和while 干完循环之后执行else,干不完或者break就不执行 3.else与异常处理 没有问题的话就执行else吧
- Spring(3)——装配 Spring Bean 详解
装配 Bean 的概述 前面已经介绍了 Spring IoC 的理念和设计,这一篇文章将介绍的是如何将自己开发的 Bean 装配到 Spring IoC 容器中. 大部分场景下,我们都会使用 Appl ...
- geotrellis使用(三十九)COG 写入更新
前言 前面介绍过了如何在 ETL 的时候更新 Layer,使得能够在大数据量的时候完成 ETL 操作,同时前两篇文章也介绍了 COG 以及如何在 Geotrellis 中实现 COG 的读取.本文介绍 ...
- [LeetCode] Binary Number with Alternating Bits 有交替位的二进制数
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...
- [LeetCode] Longest Uncommon Subsequence I 最长非共同子序列之一
Given a group of two strings, you need to find the longest uncommon subsequence of this group of two ...