#冒泡排序
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. Android 利用WebViewJavascriptBridge 实现js和java的交互(一)

    此文出自:http://blog.csdn.net/sk719887916/article/details/47189607,skay 按安卓开发目前现状来说,开发者大部分时间还是花在UI的屏幕适配上 ...

  2. Java进阶(五十一)Could not create the view: An unexpected exception was thrown

    Java进阶(五十一)Could not create the view: An unexpected exception was thrown 今天打开Myeclipse10的时候,发现server ...

  3. gdb学习(一)[第二版]

    概述 gdb是GNU debugger的缩写,是编程调试工具. 功能 1.启动程序,可以按照用户自定义的要求随心所欲的运行程序. 2.可让被调试的程序在用户所指定的断点处停住 (断点可以是条件表达式) ...

  4. 为什么选择Nginx

     Nginx 是一个高性能的 Web 和反向代理服务器, 它具有有很多非常优越的特性: 作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率,这 ...

  5. 程序员编程艺术:第三章续、Top K算法问题的实现

    程序员编程艺术:第三章续.Top K算法问题的实现 作者:July,zhouzhenren,yansha.     致谢:微软100题实现组,狂想曲创作组.     时间:2011年05月08日    ...

  6. 带三方登录(qq,微信,微博)

    实现QQ.微信.新浪微博和百度第三方登录(Android Studio) 前言:  对于大多数的APP都有第三方登录这个功能,自己也做过几次,最近又有一个新项目用到了第三方登录,所以特意总结了一下关于 ...

  7. TCP连接建立系列 — 服务端接收SYN段

    本文主要分析:服务器端接收到SYN包时的处理路径. 内核版本:3.6 Author:zhangskd @ csdn blog 接收入口 1. 状态为ESTABLISHED时,用tcp_rcv_esta ...

  8. iOS中获取本地通讯录联系人以及汉字首字母排序

    iOS中获取手机通讯录中的联系人信息: /*** 加载本地联系人*/ - (void)loadLocalContacts { //新建一个通讯录类 ABAddressBookRef addressBo ...

  9. Android Studio Gradle Configuration Errors总结

    初次看到这个错误,我从下手Error:Configuration with name 'default' not found.  只知道这是由于android的grad项目构建的时候出现的错误,但是具 ...

  10. "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: no freetype in java.library.path

    Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: no freetype in java ...