#冒泡排序
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. ShapeDrawable做放大镜效果

    引用一下ShapeDrawable的类的说明: java.lang.Object    ? android.graphics.drawable.Drawable      ? android.grap ...

  2. 【Android 应用开发】Ubuntu 下 Android Studio 开发工具使用详解 (旧版本 | 仅作参考)

    . 基本上可以导入项目开始使用了 ... . 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/21035637 ...

  3. 反对抄袭 正解spring的@Autowired 不要相信网上的错误版本

    首先,最重要的, @Autowired的就是用来来消除 set ,get方法. 有些介绍,如著名的马士兵,说要在set方法上进行注入.我当时就看不明白了,既然只取消了一个GET,这个@Autowire ...

  4. startService与bindService的区别

    转自:http://www.devdiv.com/thread-52226-1-1.html Service的生命周期方法比Activity少一些,只有onCreate, onStart, onDes ...

  5. 安装nodejs中遇到的问题

    make clean 出现时间不对的问题的时候,主要是系统时间设置错误. 用date -s 来分别设置日期和时间 用clock -w来写入CMS 安装g++ yum -y install gcc-c+ ...

  6. C++对txt文本进行读写操作

    输入输出,是每个程序员的基本功,尤其是对文本的输入和输出.最近,自己在这方面做了一些总结,不是很全面,希望在以后学习和工作的过程中慢慢补充,积累点点滴滴.P.S. 今天天气不错,雾霾散了,天空晴朗,惠 ...

  7. Windows7驱动调试小Tips

    v:* { } o:* { } w:* { } .shape { }p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-botto ...

  8. OpenCV实现图像物体轮廓,前景背景,标记,并保存。

    #include <iostream> // for standard I/O #include <string> // for strings #include <io ...

  9. leetcode(58)-Range Sum Query - Immutable

    题目: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...

  10. Jmeter(二十四)_服务器性能监控

    下载插件 1.访问网址http://jmeter-plugins.org/downloads/all/,下载三个文件.其中JMeterPlugins-Standard和JMeterPlugins-Ex ...