#冒泡排序
array = [1,2,3,6,5,4]
for i in range(len(array)):
for j in range(i):
if array[j] > array[j + 1]:
array[j], array[j + 1] = array[j + 1], array[j]
print(array)
#字符串格式化用法
x=123
so="%o"%x #8进制
print(so)
sh = "%x"%x #16进制
print(sh)
se='%e'%x
print(se)
print('%s'%65) #等价于str()
print('%s'%65333)
print('%d,%c'%(65,65)) #使用元组对字符串进行格式化,按位置进行对应
print(int('')) #可以使用int()函数将合法的数字字符串转化为整数
print('%s'%[1,2,3])
print(str([1,2,3])) #可以使用str()函数将任意类型数据转换为字符串
print(str((1,2,3)))
print(str({'A':65,'B':66})) #使用format()方法进行格式化,该法官法更加灵活,不仅可以使用位置进行格式化,还支持使用与位置无关的参数名来进行格式化,并且支持
#序列解包格式化字符串,为程序员提供非常大的方便
print('{0:.3f}'.format(1/3)) #保留3位小数
# 0.333
print("The number {0:,} in hex is: {0:#x},i oct is {0:#o}".format(55))
# The number 55 in hex is: 0x37,i oct is 0o67
print('The number {0:,} in hex is: {0:x},the number {1} inn oct is {1:o}'.format(555,55))
#The number 555 in hex is: 22b,the number 55 inn oct is 67
print('THe number {1} in hex is: {1:#x},the number {0} in oct is {0:#o}'.format(5555,55))
# THe number 55 in hex is: 0x37,the number 5555 in oct is 0o12663
print('My name is {name},my age is {age},and my QQ is {qq}'.format(name='zWrite',qq='122668xx',age=25))
# My name is zWrite,my age is 25,and my QQ is 122668xx
position =(5,6,12)
print('X:{0[0]};Y:{0[1]};Z:{0[2]}'.format(position)) #使用元组同时格式化多值
# X:5;Y:6;Z:12
weather = [('Monday','rain'),('Tuesday','sunny'),('Wednessday','sunny'),('Thursday','rain'),('Friday','cloudy')]
formatter='Weather of "{0[0]}" is "{0[1]}"'.format
for item in map(formatter,weather):
print(item)
# Weather of "Monday" is "rain"
# Weather of "Tuesday" is "sunny"
# Weather of "Wednessday" is "sunny"
# Weather of "Thursday" is "rain"
# Weather of "Friday" is "cloudy"
from string import Template
t = Template('My name is ${name},and is ${age} years old.') #创建模版
d = {'name':'zWrite','age':39}
t.substitute(d) #替换
print(t)
# <string.Template object at 0x101389198>
tt = Template('My name is $name,and is $age years old.')
tt.substitute(d)
print(tt)
# <string.Template object at 0x101389588>

Python_字符串格式化的更多相关文章

  1. 1.Python_字符串_常用办法总结

    明确:对字符串的操作方法都不会改变原来字符串的值. 1.去掉空格和特殊符号 name.strip() 去掉空格和换行符 name.strip("xx") 去掉某个字符串 name. ...

  2. Python高手之路【六】python基础之字符串格式化

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...

  3. Java 字符串格式化详解

    Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...

  4. Python(四)装饰器、迭代器&生成器、re正则表达式、字符串格式化

    本章内容: 装饰器 迭代器 & 生成器 re 正则表达式 字符串格式化 装饰器 装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志.性能测试.事务处理等.装饰器是解 ...

  5. C++字符串格式化库:CPPFormatLibrary

    这个是很久之前写的,去年总结了一下,将其单独提取出来,作为一个开源库放到了GitHub上,然而CPPFormat之类的名字都已经被抢注了,结果只好注册了一个这么蛋疼的名字:CPPFormatLibra ...

  6. js字符串格式化扩展方法

    平时使用js的时候会遇到很多需要拼接字符串的时候,如果是遇到双引号和单引号混合使用,经常会搞混.在C#中有string.Format方法,使用起来非常方便,也很容易理解,所以找到一种参考C#的form ...

  7. JAVA字符串格式化-String.format()的使用(转)

    常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得C语言的sprintf()方法,两者有类似之处.format()方法有两种重 ...

  8. Python字符串格式化

    一.使用格式化符来格式化字符串: Python支持的所有格式化符 格式化符 意义 'd' 返回要格式化对象的十进制表示,如果可以 'i' 返回要格式化对象的十进制表示,如果可以 'o' 返回要格式化对 ...

  9. Python 字符串格式化

    Python 字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存 一 ...

随机推荐

  1. 2013 HTML5中国峰会演讲:Android上的HTML5:过去,现在和将来

    转载请注明原文地址:http://blog.csdn.net/milado_nju ## 会议链接(应用和工具专场) http://2013.html5dw.com/main, 2013年8月10日 ...

  2. (NO.00001)iOS游戏SpeedBoy Lite成形记(二)

    打开SpriteBuilder生成的Xcode项目文件,首先在MainScene添加play回调函数: -(void)play{ CCScene *gameScene = [CCBReader loa ...

  3. JavaScript进阶(二)在一个JS文件中引用另一个JS文件

    在一个JS文件中引用另一个JS文件       转载地址:http://blog.csdn.net/zndxlxm/article/details/7875787 方法一 在调用文件的顶部加入下例代码 ...

  4. 支持老版本IE的3种解决方案

    原文链接: 3 Solutions for Supporting Internet Explorer 翻译人员: 铁锚 翻译时间: 2014年2月14日 在刚发布的那几年,Internet Explo ...

  5. UE4 创建第三人称角色

    一.C++创建 1.      创建一个C++类,继承自Character,取名MyThirdCharacter 2.      在头文件中声明一个摄像机目标点CameraBoom,一个摄像机Foll ...

  6. Oracle EBS BOM模块常用表结构

    表名: bom.bom_bill_of_materials  说明: BOM清单父项目  BILL_SEQUENCE_ID NUMBER 清单序号(关键字)ASSEMBLY_ITEM_ID NUMBE ...

  7. 通过服务修改widgetUI

    public static void updateAppWidget(Context context, String displayMsg) { AppWidgetManager appWidgetM ...

  8. Linux备份策略(第二版)

    备份策略 备份思想 一.系统潜在的威胁 Ø 系统硬件故障 Ø 软件故障 Ø 电源故障 Ø 用户的误操作 Ø 人为破坏 Ø 缓存中的内容没有及时的写入磁盘 Ø 自然灾害 二.备份介质的选择 备份介质:硬 ...

  9. Swift之GCD 使用指南2

    Grand Central Dispatch大中枢派发:joy: 或俗称 GCD 是一件极其强大的武器.它为你提供了很多底层工具(比如队列和信号量),你可以组合这些工具来实现自己想要的多线程效果.不幸 ...

  10. 安卓TV开发(九) Android模拟事件 遥控器变身成鼠标来操作TV

    本文出处:http://blog.csdn.net/sk719887916/article/details/40348853,作者:skay      阅读此文建议先阅读 安卓Tv开发(二)移动智能电 ...