python3 开发面试题(%s和format的区别)5.31
在格式化字符串中有两种方法:
1、%s
2、format
大家常用的是哪一种方法?为什么要用你选的这种方法?
我们先看一个例子:
首先我们定义一个我军需要击杀的恐怖分子的地理坐标为 c=(128,128)
# 利用%进行字符串格式化
print("向他开炮:%s" % c) #报错 TypeError: not all arguments converted during string formatting print("向他开炮:%s" % (c, )) #正确的写法 # 用format进行字符串的格式化
print("向他开炮:{}".format(c))
format的常见方法:
#通过关键字
d1 = {"name": "duoduo", "age": 18}
# s = "{name} is {age} years old.".format(d1["name"], d1["age"])
s = "{name} is {age} years old.".format(**d1)
print(s) #通过对象属性
class Person(): def __init__(self, name, age):
self.name = name
self.age = age def __str__(self):
return "{self.name} - {self.age}".format(self=self) p1 = Person("duoduo", 18)
print(p1) #通过下标
l1 = ["duoduo", 18]
# s = "{} is {} years old. {} 帅。".format(l1[0], l1[1], l1[0])
# s = "{0} is {1} years old. {0} 帅。".format(l1[0], l1[1])
s = "{0[0]} is {0[1]} years old. {0[0]} 帅。".format(l1)
print(s) #填充与对齐
print("duoduo".center(20, "*")) #填充左右
print("{:>10}".format("duoduo")) #左边空格填充
print("{:0>10}".format("duoduo")) #左边0填充
print("{:*>10}".format("duoduo")) #左边*填充
print("{:*^10}".format("duoduo")) #左右填充
print("{:*<10}".format("duoduo")) #右边用*填充 print("duoduo".zfill(18)) # 0填充 print("{:.2f}".format(3.141592653)) #保留两位小数
print("{:b}".format(10)) #二进制
print("{:d}".format(10)) #十进制
print("{:o}".format(10)) #八进制
print("{:x}".format(10)) #十六进制 print("{:,}".format(1000000)) # 千分位格式化
python3 开发面试题(%s和format的区别)5.31的更多相关文章
- python3 开发面试题(面向对象)6.6
""" 封装.继承.多态 1. 谈谈你对面向对象的理解? 2. Python面向对象中的继承有什么特点? 3. 面向对象深度优先和广度优先是什么? 4. 面向对象中sup ...
- python3 开发面试题(collections中的Counter)6.7
''' 编写Python脚本,分析xx.log文件,按域名统计访问次数 xx.log文件内容如下: https://www.sogo.com/ale.html https://www.qq.com/3 ...
- python3 开发面试题(创建表结构)6.9
纯sql语句写出: '''设计 图书管理系统 表结构: - 书 - 书名 - 作者 - 姓名 - 出版社 - 出版社名称 - 地址 一本书只能由一家出版社出版 --> 多对一(书对出版社) 一本 ...
- python3 开发面试题(常用模块以及第三方库)6.5
""" 1. os和sys都是干什么的? 2. 你工作中都用过哪些内置模块? 3. 有没有用过functools模块? """ #sys模块 ...
- python3 开发面试题(装饰器必须考)6.4
def f(): print("2018-06-04") # 每次调用f的时候 在打印"2018-06-04" 之前打印一句 开始, 之后再打印一句 结束 de ...
- python3 开发面试题(去重保持原来的顺序)6.2
""" l1 = [11, 2, 3, 22, 2, 4, 11, 3] 去重并保持原来的顺序 """ #方式一 for 循环方法 l1 = ...
- python3 开发面试题(字典和拷贝)5.30
""" 问:执行完下面的代码后, l,m的内容分别是什么? """ def func(m): for k,v in m.items(): m ...
- python3 开发面试题(生成列表)6.1
话不多说直接上题: 生成如下列表: [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8], [0, 3, 6, 9, 12]] # 方式一 list1 ...
- 【理论面试篇】收集整理来自网络上的一些常见的 经典前端、H5面试题 Web前端开发面试题
##2017.10.30收集 面试技巧 5.1 面试形式 1) 一般而言,小公司做笔试题:大公司面谈项目经验:做地图的一定考算法 2) 面试官喜欢什么样的人 ü 技术好. ...
随机推荐
- Nginx support TCP Load balance
1. Install nginx package 2. edit nginx configuration file [root@ip- nginx]# more nginx.conf user ngi ...
- ICE学习笔记一----运行官方的java版demo程序
建议新手和我一样,从官网下载英文文档,开个有道词典,慢慢啃. 官方文档下载: http://download.csdn.net/detail/xiong_mao_1/6300631 程序代码就不说了, ...
- linux网络编程系列-TCP/IP模型
### OSI:open system interconnection ### 开放系统互联网模型是由ISO国际标准化组织定义的网络分层模型,共七层 1. 物理层:物理定义了所有电子及物理设备的规范, ...
- css sprite应用
(一)实现简单的淘宝带图标侧边栏效果 <!DOCTYPE html> <html lang="en"> <head> <meta char ...
- php连接mysql报错——Fatal error: Call to undefined function mysql_connect() in
练习php连接mysql数据库 代码:mysql_connect("127.0.0.1:3306","root", ..... 浏览器报错:Fatal erro ...
- C++中的垃圾回收和内存管理
最开始的时候看到了许式伟的内存管理变革系列,看到性能测试结果的时候,觉得这个实现很不错,没有深入研究其实现.现在想把这个用到自己的一个项目中来,在linux下编译存在一些问题,所以打算深入研究一下. ...
- 【BZOJ2286】【SDOI2011】消耗战 [虚树][树形DP]
消耗战 Time Limit: 20 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 在一场战争中,战场由n个岛屿和n-1 ...
- Codeforces Round #433
我会4题,写了两题,还提交错误n次,掉了40rating(哭丧脸),又被学长D飞了. 学长:我很心疼你的成绩啊: 我:第四题忘记加特判了... 学长:暴力还能写挂. 我:...... ———————— ...
- udp端口测试连接
udp端口测试连接 http://www.361way.com/nc-udp-port/2949.html
- JMeter之定时器的作用域
定时器的作用域 1.定时器是在每个sampler(采样器)之前执行的,而不是之后(无论定时器位置在sampler之前还是下面): 2.当执行一个sampler之前时,所有当前作用域内的定时器都会被执行 ...