Python Strings

Strings are set of contiguous characters represented starting and ending with a quotation mark.

single_quotation_mark = ‘word’
double_quotation_mark = “word”
triple_quotation_mark = “”” Wohoooooooo!!! Python is a

very awesome and fun language.”””

Python allows for either pairs of single or double quotes or even triple quotes to declare a string. Subsets of strings can be taken using the slice operator ([ ] and [:] ) where 0 indicate the starting of the string and -1 indicates the endind of the string.

learn_python = ‘python is a very awesome language’

print learn_python[0:5] #prints ‘python’ on the console

print learn_python #prints the whole string

print learn_python[5:] #prints string starting from 6th character

The plus (+) sign is the string concatenation operator and the asterisk (*) is the repetition operator. For example −
print learn_python * 2      # Prints string two times
print learn_python + “Wohooooooooo” # Prints concatenated string

Python Lists

List is the backbone of Python. A list contains items separated by commas and enclosed within square brackets ([]). To some extent, lists are similar to arrays in C. One difference between them is that all the items belonging to a list can be of different data type whereas in C, the data type should be mentioned while declaring an array.

The values stored in a list can be accessed using the slice operator ([ ] and [:]) where 0 indicates the beginning of the string and -1 indicates the end of the string. The plus (+) sign is the list concatenation operator which is used to join two or more strings, and the asterisk (*) is the repetition operator which is used to combine strings multiple times. append() function is used to add element to the end of the list and range(n) function iterates from 0 to n-1.

for : Suppose you want to shoot the basket 100 times. For that you have to write  shoot_basket() function 100 times which is very cumbersome process. Instead of writing function 100 times, We’ll use for  loop to run shoot_basket() function 100 times.

for i in range(100):



Question: Write a function to append a list of [“O”]*5 elements in another list.

Recommended Post   While Loop Python

Solution :


for i in range(5):


This produce the following result −

[[‘O’, ‘O’, ‘O’, ‘O’, ‘O’], [‘O’, O’, ‘O’, ‘O’, ‘O’], [‘O’, ‘O’, ‘O’, ‘O’, ‘O’], [‘O’, ‘O’, ‘O’, ‘O’, ‘O’], [‘O’, ‘O’, ‘O’, ‘O’, ‘O’]]


Question : Define a function which can be used to display the above function as each separate list. The o/p should look like

[‘O’, ‘O’, ‘O’, ‘O’, ‘O’]
[‘O’, ‘O’, ‘O’, ‘O’, ‘O’]
[‘O’, ‘O’, ‘O’, ‘O’, ‘O’]
[‘O’, ‘O’, ‘O’, ‘O’, ‘O’]
[‘O’, ‘O’, ‘O’, ‘O’, ‘O’]
HINT: Use ‘for’ loop to iterate over each boat and print each boat (or list) that is within the outer list.


Please enter your comment!
Please enter your name here