python 打印列表元素
In this program – we are going to learn how can we print all list elements, print specific elements, print a range of the elements, print list multiple times (using * operator), print multiple lists by concatenating them, etc.
在此程序中,我们将学习如何打印所有列表元素 ,打印特定元素,打印元素范围,多次打印列表(使用*运算符),通过将它们串联来打印多个列表,等等。
Syntax to print list in different ways:
以不同方式打印列表的语法:
print (list) # printing complete list
print (list[i]) # printing ith element of list
print (list[i], list[j]) # printing ith and jth elements
print (list[i:j]) # printing elements from ith index to jth index
print (list[i:]) # printing all elements from ith index
print (list * 2) # printing list two times
print (list1 + list2) # printing concatenated list1 & list2
Python代码打印列表元素 (Python code to print list elements )
Here, we have two lists list1 and list2 with some of the elements (integers and strings), we are printing elements in the different ways.
在这里,我们有两个列表list1和list2 ,其中包含一些元素(整数和字符串),我们以不同的方式打印元素。
# python program to demonstrate example of lists
# declaring & initializing two list
list1 = ["Amit", "Abhi", "Radib", 21, 22, 37]
list2 = [100, 200, "Hello", "World"]
print (list1) # printing complete list1
print (list1[0]) # printing 0th (first) element of list1
print (list1[0], list1[1]) # printing first & second elements
print (list1[2:5]) # printing elements from 2nd to 5th index
print (list1[1:]) # printing all elements from 1st index
print (list2 * 2) # printing list2 two times
print (list1 + list2) # printing concatenated list1 & list2
Output
输出量
['Amit', 'Abhi', 'Radib', 21, 22, 37]
Amit
Amit Abhi
['Radib', 21, 22]
['Abhi', 'Radib', 21, 22, 37]
[100, 200, 'Hello', 'World', 100, 200, 'Hello', 'World']
['Amit', 'Abhi', 'Radib', 21, 22, 37, 100, 200, 'Hello', 'World']
翻译自: https://www.includehelp.com/python/print-list-elements-in-different-ways.aspx
python 打印列表元素