Python基础篇(格式化输出,运算符,编码): 格式化输出: 格式:print ( " 内容%s" %(变量)) 字符类型: %s 替换字符串 %d 替换整体数字 %f替换浮点型 ------------ info of Alex Li ----------- ------------ info of %s ----------- Name : Alex Li …
Python 入门之格式化输出 1.格式化 (1)%为占位 (2)%s --- 站字符串的位置(数字.字符串都能够进行填充) name = input('请输入姓名:') age = input('请输入年龄:') job = input('请输入职业:') hobby = input('请输入爱好:') msg = ''' ------------ info of Alex Li ---------- Name : %s Age : %s job : %s Hobbie: %s -------…
[Python练习题 006] 输出九九乘法表 --------------------------------------------------- 照理这题不难,逻辑关系弄对了就好办,但数学渣的我就是想了好一会儿再写对的啊-- 代码如下: for i in range(1,10): #循环9次,打印出9行 for j in range(1,i+1): #第几行就有几项 print('%s*%s = %s' % (j, i, i*j), end=' ') print('') #每循环1次就打印…
问题1:如果我们知道汉字编码范围是0x4E00到0x9FA5,怎么从十六进制的编码转成人类可读的字呢? 问题2:怎么把unicode编码的字写入文件呢,假设直接用open()的话,会提示UnicodeEncodeError: 'ascii' codec can't encode character u'\u4e00' in position 0: ordinal not in range(128) 问题1的答案是用unichr,问题2的答案是用codes. 以下上代码. import codec…
1. 格式化输出 现在有以下需求,让⽤户输入name, age, job,hobby 然后输出如下所⽰: ------------ info of Alex Li ----------- Name : Alex Li Age : 22 job : Teacher Hobbie: girl ------------- end ----------------- 你怎么实现呢?你会发现,⽤字符拼接的⽅式还难实现这种格式的输出,所以⼀起来学⼀下新知识,只需要把要打印的格式先准备好, 由于⾥⾯的⼀些信息…
[1]a=[8,13,11,6,26,19,24]1)请输出列表a中的奇数项2)请输出列表a中的奇数 解:1) a=[8,13,11,6,26,19,24] print a[::2] Result:>>>[8, 11, 26, 24] 2) a = [8,13,11,6,26,19,24] b = [] for item in a: if item%2 !=0: b.append(item) else: continue print b Result:>>>[13, 1…
前面已经说过了,print()函数括号里加上字符串,就可以实现输出 >>> print('This is Python!') This is Python! print()函数也可以接受多个字符串,用,隔开 >>> print('This is python!','It is a programming language.') This is python! It is a programming language 打印的时候,碰到逗号就会输出一个空格. 其实还可以这样…