Home » Python » Python programs

Python program to print list elements in different ways

Printing list elements in Python: Here, we are going to learn how to print list elements in different ways?
Submitted by IncludeHelp, on April 09, 2019

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 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.

# 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']





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.