You are on page 1of 3

Nested Loops

A loop may contain another loop inside it. A loop inside another loop is called a nested loop.
Example1:
for var1 in range(3):
print( "Iteration " + str(var1 + 1) + " of outer loop")
for var2 in range(2): #nested loop
print(var2 + 1)
print("Out of inner loop")
print("Out of outer loop")
Example2: Program to print the pattern for a number input by the user.
num = int(input("Enter a number to generate its pattern = "))
for i in range(1,num + 1):
for j in range(1,i + 1):
print(j, end = " ")
print()
#The following program uses a for loop nested inside an if….else block to calculate the factorial
of a given number
num = int(input("Enter a number: "))
fact = 1
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1, num + 1):
fact = fact * i
print("factorial of ", num, " is ", fact)
Strings
String is a sequence which is made up of one or more UNICODE characters. Here the character can
be a letter, digit, whitespace or any other symbol. A string can be created by enclosing one or more
characters in single, double or triple quote.
Example
str1 = 'Hello World!'
str2 = "Hello World!"
str3 = """Hello World!"""
str1, str2, str3 are all string variables having the same value 'Hello World!'.Values stored in
str3 can be extended to multiple lines using triple codes as can be seen in the following example:
str3 = """Hello World!
welcome to the world of Python"""
Strings are immutable.
A string is an immutable data type. It means that the contents of the string cannot be changed
after it has been created. An attempt to do this would lead to an error.
str1 = "Hello World!"
#if we try to replace character 'e' with 'a'
str1[1] = 'a'
TypeError: 'str' object does not support item assignment.
Accessing Characters in a String
Each individual character in a string can be accessed using a technique called indexing. The index
specifies the character to be accessed in the string and is written in square brackets [ ]. The index of
the first character (from left) in the string is 0 and the last character is n-1 where n is the length of
the string. If we give index value out of this range then we get an IndexError. The index must be an
integer (positive, zero or negative).
Example:
str="Computer Science"
print("str",str)
print("str[0]",str[0])
print("str[0:4]",str[0:4])
print("str[2:6]",str[2:6])
print("str[:]",str[:])
print("str[:4]",str[:4])
print("str*3",str*3)
print(str+' Informatics Practices')
Iteration through string
str="Computer Science"
for i in str:
print(i)
Updating String: String can be updated by reassigning another value to it.
str1 = "Hello! "
str1 = str1+'How are you?'
print(str1)

Eg. sname="abhishek"
rollno=101
marks=88.50
print("hello my name is %s, roll no is %d and i got %.2f marks"%(sname,rollno,marks))
Functions/Methods
len() : Returns the length of the given string.
str1 = 'this is a python!'
print("Total length of the string is",len(str1))
capitalize() : To capitalize the string. First letter of the string become capital.
title() : Returns the string with first letter of every word in the string in uppercase and rest in
lowercase.
upper() : Returns the string with all lowercase letters converted to uppercase.
lower() : Returns the string with all uppercase letters converted to lowercase.
str1 = 'this is a python!'
print("Captitalize case :",str1.capitalize())
print("Title case :",str1.title())
print("Upper case :",str1.upper())
print("Lower case :",str1.lower())
count(str, start, end): Returns number of times substring str occurs in the given string. If we do not
give start index and end index then searching starts from index 0 and ends at length of the string.
str1 = 'this is a python!'
print("Occurence of is in string",str1.count('is'))
print("Occurence of is in string from index 4 to 16",str1.count('is',4,16))
find(str, start, end) : Returns the first occurrence of index of substring str occurs in the given string.
If we do not give start and end then searching starts from index 0 and ends at length of the string.
If the substring is not present in the given string, then the function returns -1.
str1 = 'this is a python!'
print("First Occurence of index of (is) in string",str1.find('is'))
print("First Occurence of index of (is) in string from index 4 to 16",str1.find('is',4,16))
print("First Occurence of index of (is) in string from index 7 to 16",str1.find('is',7,16))
print("First Occurence of index of (hello) in string",str1.find('hello'))
index(str, start, end) : Same as find() but raises an exception if the substring is not present in the
given string.
str1 = 'this is a python!'
print("Index of is",str1.index('is'))
print("Index of is",str1.index('iss'))
isalnum() : Returns True if characters of the given string are either alphabets or numeric. If
whitespace or special symbols are part of the given string or the string is empty it returns False.
str1 = 'HelloWorld'
print(str1.isalnum())
str1 = 'HelloWorld2'
print(str1.isalnum())
str1 = 'HelloWorld!!'
print(str1.isalnum())
str1 = 'Hello World'
print(str1.isalnum())

You might also like