strHello = 'Hello Python' print strHello # Hello Python
strHello = "the length of (%s) is %d" %('Hello Wordld', len('Hello World'))
print strHello
# the length of (Hello Wordld) is 11
# 格式 描述 # %% 百分号标记 # %c 字符及其ASCII码 # %s 字符串 # %d 有符号整数(十进制) # %u 无符号整数(十进制) # %o 无符号整数(八进制) # %x 无符号整数(十六进制) # %X 无符号整数(十六进制大写字符) # %e 浮点数字(科学计数法) # %E 浮点数字(科学计数法,用E代替e) # %f 浮点数字(用小数点符号) # %g 浮点数字(根据值的大小采用%e或%f) # %G 浮点数字(类似于%g) # %p 指针(用十六进制打印值的内存地址) # %n 存储输出字符的数量放进参数列表的下一个变量中
nHex = 0x20 print 'nHex = %x, nDec = %d, nOct = %o' %(nHex, nHex, nHex) # nHex = 20, nDec = 32, nOct = 40
# Python 2.7.10 (default, Feb 7 2017, 00:08:15) # [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin # Type "help", "copyright", "credits" or "license" for more information. # >>> bin(789) # '0b1100010101' # >>>
import math #default print 'PI = %f' % math.pi # PI = 3.141593 # width = 10, precise = 3, align = left print 'PI = .3fxxx' % math.pi # PI = 3.142xxx # width = 10, precise = 3, align = right print 'PI = %-10.3fxxx' % math.pi # PI = 3.142 xxx # 前面填充字符串 print 'PI = d' % int(math.pi) # PI = 000003
# precise = 3
print '%.3s' % ('jcodeer')
# jco
# precise = 4
print '%.*s' % (4,'jcodeer')
# jcod
# width = 10, precise = 3
print 'xx.3s' % ('jcodeer')
# xx jco
l = [1, 2, 3, 'jcodeer'] print l # [1, 2, 3, 'jcodeer']
d = {1: 'A',2: 'B',3: 'C',4: 'D'}
print d
# {1: 'A', 2: 'B', 3: 'C', 4: 'D'}
# print会在行末加上回车,如果不需要,只需在print语句结尾添加一个逗号',' for i in range(0,5): print i, # 0 1 2 3 4
import sys
sys.stdout.write("输出的字符串")
formatter = '%r %r %r %r'
print formatter % (1, 2, 3, 4)
print formatter % ('one', 'two', 'three', 'four')
print formatter % (True, False, False, True)
print formatter % (formatter, formatter, formatter, formatter)
print formatter % (
"I had this thing.",
"That you could type up right.",
"But it didn't sing.",
"So I said goodnight."
)
# 1 2 3 4
# 'one' 'two' 'three' 'four'
# True False False True
# '%r %r %r %r' '%r %r %r %r' '%r %r %r %r' '%r %r %r %r'
# 'I had this thing.' 'That you could type up right.' "But it didn't sing." 'So I said goodnight.'
import numpy as np a = np.array([[1,2],[3,4]]) b = np.array([[5,6],[7,8]]) print a # [[1 2] # [3 4]] print b # [[5 6] # [7 8]] print a, b # [[1 2] # [3 4]] [[5 6] # [7 8]]
# {0}表示第一个元素, {1}表示第二个元素, {2}表示第三个元素,以此类推。。。
a = 'Ace'
b = 'hello'
print("{1}, my name is {0}".format(a, b))
# hello, my name is Ace
name = "Ace"
age = 26
print("{myname}'s age is {myage}".format(myname=name, myage=age))
# Ace's age is 26
person = ["Ace", 26]
print("{0[0]}'s age is {0[1]}".format(person))
# Ace's age is 26
print("{people[0]}'s age is {people[1]}".format(people=person))
# Ace's age is 26
person = {'Ace': 26}
print("{myname}'s age is {people[Ace]}".format(myname=name,people=person))
# Ace's age is 26
a = 123.456789
haha = 'haha!!!'
print("{0:0.3f}, *{1:<14}*".format(a, haha))
print("{0:0.3f}, *{1:>14}*".format(a, haha))
print("{0:0.3f}, *{1:^14}*".format(a, haha))
print("{0:0.3f}, *{1:}*".format(a, haha))
# 123.457, *haha!!! *
# 123.457, * haha!!!*
# 123.457, * haha!!! *
# 123.457, *haha!!!*
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有