You are on page 1of 8

7/27/22, 9:42 AM Python | Set 3 (Strings, Lists, Tuples, Iterations) - GeeksforGeeks

Data Structures Algorithms Interview Preparation Topic-wise Practice C++ Java Python

Python | Set 3 (Strings, Lists, Tuples,


Iterations)
Difficulty Level :
Easy ● Last Updated :
13 Jul, 2022

In the previous ar ticle, we read about the basic s of P ython. Now, we continue with

some more python concepts.

Strings in P ython: 

A string is a sequence of characters that can be a combination of letters, numbers,

and special characters. It can be declared in python by using single quotes, double

quotes, or even triple quotes. These quotes are not a par t of a string, they define only

star ting and ending of the string.   Strings are immutable, i.e., they cannot be

changed. Each element of the string can be accessed using indexing or slicing

operations.

0:00 to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
We use cookies
that you have read and understood our
Cookie Policy &
Privacy Policy

Got It !

https://www.geeksforgeeks.org/python-set-3-strings-lists-tuples-iterations/?ref=lbp 1/8
7/27/22, 9:42 AM Python | Set 3 (Strings, Lists, Tuples, Iterations) - GeeksforGeeks

Start Your Coding Journey Now!


P ython Login Register

# Assigning string to a variable


a = 'This is a string'
print (a)
b = "This is a string"
print (b)
c= '''This is a string'''
print (c)

Output :

This is a string

This is a string

This is a string

Lists in P ython:

Lists are one of the most power ful data structures in python. Lists are sequenced

data types.   In P ython, an empty list is created using list() function. They are just like

the arrays declared in other languages. But the most power ful thing is that list need

not be always homogeneous. A single list can contain strings, integers, as well as

other objects. Lists can also be used for implementing stacks and queues. Lists are

mutable, i.e., they can be altered once declared. The elements of list can be accessed

using indexing and slicing operations.

P ython

# Declaring a list
L = [1, "a" , "string" , 1+2]
print L
#Adding an element in the list
L.append(6)    
print L
#Deleting last element from a list
L.pop()
print L
#Displaying Second element of the list
print L[1]
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
that you have read and understood our
Cookie Policy &
Privacy Policy

Got It !

https://www.geeksforgeeks.org/python-set-3-strings-lists-tuples-iterations/?ref=lbp 2/8
7/27/22, 9:42 AM Python | Set 3 (Strings, Lists, Tuples, Iterations) - GeeksforGeeks

Start Your Coding Journey Now!


The output is:  

Login Register
[1, 'a', 'string', 3]

[1, 'a', 'string', 3, 6]

[1, 'a', 'string', 3]

Tuples in P ython: A tuple is a sequence of immutable P ython objects. Tuples are just

like lists with the exception that tuples cannot be changed once declared. Tuples are

usually faster than lists.

P ython

tup = (1, "a", "string", 1+2)


print(tup)
print(tup[1])

The output is : 

(1, 'a', 'string', 3)

Iterations in P ython: Iterations or looping can be per formed in python by ‘for ’ and

‘while’ loops. Apar t from iterating upon a par ticular condition, we can also iterate on

strings, lists, and tuples.

Example 1: Iteration by while loop for a condition

P ython

i = 1
while (i < 10):
    print(i)
    i += 1

The output is: 

We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
1

that you have read and understood our


Cookie Policy &
Privacy Policy
2

Got It !
3

https://www.geeksforgeeks.org/python-set-3-strings-lists-tuples-iterations/?ref=lbp 3/8
7/27/22, 9:42 AM Python | Set 3 (Strings, Lists, Tuples, Iterations) - GeeksforGeeks

Start
5
Your Coding Journey Now! Login Register
6

Example 2: Iteration by for loop on the string

P ython

s = "Hello World"
for i in s:
    print(i)

The output is: 

Example 3: Iteration by for loop on list

P ython

L = [1, 4, 5, 7, 8, 9]
for i in L:
    print(i)

We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
that you have read and understood our
Cookie Policy &
Privacy Policy
The output is: 

Got It !

https://www.geeksforgeeks.org/python-set-3-strings-lists-tuples-iterations/?ref=lbp 4/8
7/27/22, 9:42 AM Python | Set 3 (Strings, Lists, Tuples, Iterations) - GeeksforGeeks

Start
1

Your Coding Journey Now! Login Register


4

Example 4: Iteration by for loop for range

P ython

for i in range(0, 10):


    print(i)

The output is: 

Next Ar ticle – P ython: Dictionar y and Keywords

Quiz on Data Types in P ython

We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
that you have read and understood our
Cookie Policy &
Privacy Policy

Got It !
Like 219

https://www.geeksforgeeks.org/python-set-3-strings-lists-tuples-iterations/?ref=lbp 5/8
7/27/22, 9:42 AM Python | Set 3 (Strings, Lists, Tuples, Iterations) - GeeksforGeeks

Start Your Coding Journey Now! Login Register

Previous Next

RECOMMENDED ARTICLES Page : 1 2 3

Python | Remove duplicate tuples Python | Count tuples occurrence


01 05
from list of tuples in list of tuples
17, Jun 19 02, Apr 19

Python | Find the tuples containing


02
the given element from a list of 06 Python | Combining tuples in list of
tuples tuples
13, May 19
14, Feb 19

Python | Remove tuples from list of Python | Convert string tuples to


03
tuples if greater than n 07
list tuples
28, Feb 19
15, Oct 19

Python | Remove tuples having


04
duplicate first value from given list Python | How to Concatenate
08
of tuples tuples to nested tuples
28, Mar 19 11, Nov 19

Ar ticle Contributed By :

GeeksforGeeks

We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
Vote for difficulty that you have read and understood our
Cookie Policy &
Privacy Policy

Current difficulty :
Easy
Got It !

https://www.geeksforgeeks.org/python-set-3-strings-lists-tuples-iterations/?ref=lbp 6/8
7/27/22, 9:42 AM Python | Set 3 (Strings, Lists, Tuples, Iterations) - GeeksforGeeks

Easy Normal Medium Hard Expert


Start Your Coding Journey Now! Login Register

Improved By : micronick_02, mandvimishra123, sheetal18june

Article Tags : python-list, python-tuple, Python, School Programming

Practice Tags : python-list

Improve Article Report Issue

Writing code in comment?


Please use ide.geeksforgeeks.org,
generate link and share the link here.

Load Comments

A-143, 9th Floor, Sovereign Corporate Tower,

Sector-136, Noida, Uttar Pradesh - 201305

feedback@geeksforgeeks.org

Company Learn
About Us Algorithms
Careers Data Structures
In Media SDE Cheat Sheet
Contact Us Machine learning
Privacy Policy CS Subjects
Copyright Policy Video Tutorials
Courses
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
that you have read and understood our
Cookie Policy &
Privacy Policy

Got It !
Languages
https://www.geeksforgeeks.org/python-set-3-strings-lists-tuples-iterations/?ref=lbp 7/8
7/27/22, 9:42 AM Python | Set 3 (Strings, Lists, Tuples, Iterations) - GeeksforGeeks

News Python
Start Your Coding Journey Now! Login
Java
Register
Top News
CPP
Technology
Golang
Work & Career
C#
Business
SQL
Finance
Kotlin
Lifestyle
Knowledge

Web Development Contribute


Web Tutorials Write an Article
Django Tutorial Improve an Article
HTML Pick Topics to Write
JavaScript Write Interview Experience
Bootstrap Internships
ReactJS Video Internship
NodeJS

@geeksforgeeks
, Some rights reserved

We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
that you have read and understood our
Cookie Policy &
Privacy Policy

Got It !

https://www.geeksforgeeks.org/python-set-3-strings-lists-tuples-iterations/?ref=lbp 8/8

You might also like