What is Lists, tuples and strings in Python?

Categories: Python

What is Lists, tuples and strings in Python?


Tuples and lists are among the most basic and versatile data structures in Python. Lists contain any kind of data at all, and the elements can be of different types (floats, int, strings, even other tuples or lists). Many functions return tuples or lists. Try out the following examples in the interpreter Heres an example showing two ways defining a list and getting at an element:

a = [1,’two’]



b = [ ]





In the second part of the example, note that a list, like everything else in Python, is in fact an ”object” with actions (called ”methods”) which you can perform by appending the method name to the object name. The mod operator is useful for making circular lists, which begin over from the first element when one reaches the end. For exammple, if a is any list, a[i%len(a)] will access the list as if it were bent around in a circle. The same trick works for any integer-indexed object.

Python distinguishes between lists and tuples. These are similar, except that lists can be modified but tuples cannot. Lists are denoted by square brackets, whereas tuples are denoted by parentheses. The above example is a list rather than a tuple. You can define a tuple, but once defined you cannot modify it in any way, either by appending to it or changing one of its elements. There are a very few cases

where Python commands specifically require a tuple rather than a list, in which case you can turn a list (say, mylist) to a tuple by using the function tuple(mylist).  

Strings are also objects, with their own set of useful methods. For example:

a = ’Five gallons of worms in a 3 gallon barrel!’


b = a.split()

print b[0],b[3],b[4]

Note that the split() method returns a list,whose elements are strings. By the way, in Python, you can use either single quotes or double quotes to enclose a string, as long as you use them consistently within any one string. There is no difference in the behavior of single quoted and double quoted strings. For strings, the + operator is concatenation, i.e. a+b is the concatenation of the two strings a and b.

It is very often useful to be able to build strings from numerical values in your script. This need often arises in formatting printout of results to look nice, or in generating filenames. Suppose a = 2 and b = 3. Then, the following example show how you can insert the values into a string:

s = ’%d + %d = %d’%(a,b,a+b)

print s


note that the ”input” to the format string must be a tuple, not a list; recall, however, that if L is a list, the function call tuple(L) will return a tuple whose elements are those of the list input as the argument. If the tuple has only one element, you can leave off the parentheses. The format code %d (or equivalently %i) converts an integer into a string. You use %f for floating point numbers, and %e for floats in scientific notation. There are other options to these format codes which give you more control over the appearance of the output, and also several additional format codes.

Top Blogs
Interview Question and Answer of Python Published at:- Basic Concepts of Python Programming Published at:- Learn Python Programming Language Published at:- Let’s know about Python basics Published at:- Let’s know about the Python Shell and idle Published at:- How to running Python Locally Published at:- What is Lists, tuples and strings in Python? Published at:- Some Modules In Python how to it works Published at:- Program control: Looping, conditionals and functions Published at:- Program control: Looping, conditionals and functions ( Part 2) Published at:- Top iOS interview questions for Beginners Published at:-
R4Rin Team
The content on R4Rin.com website is created by expert teams.