[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
Python 支持格式化字符串的输出 .尽管这样可能会用到非常复杂的表达式,但最基本的用法是将一个值插入到一个有字符串格式符 %s 的字符串中. 在 Python 中,字符串格式化使用与 C 中 sprintf 函数一样的语法. 如下实例: #!/usr/bin/python print "My name is %s and weight is %d kg!" % ('Zara', 21) 以上实例输出结果: My name is Zara and weight is 21 kg! p
字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可 字符串的格式 b = "hello itcast.cn" # 或者 b = 'hello itcast.cn' 双引号或者单引号中的数据,就是字符串 字符串连接的方法 直接通过加号(+)操作符连接 a = "str1" b = "str2" c = a + b print("a:%s" %