0% found this document useful (0 votes)
9 views4 pages

Lab 5 Strings and Scope

The document outlines a programming assignment focused on string manipulation in Python. It includes tasks such as calculating the area of a triangle, replacing repetitive characters in a string, analyzing string properties, and defining functions for various string-related operations. Additionally, it presents exercises on global and local variables, function scopes, and string characteristics like symmetry and palindrome checking.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Lab 5 Strings and Scope

The document outlines a programming assignment focused on string manipulation in Python. It includes tasks such as calculating the area of a triangle, replacing repetitive characters in a string, analyzing string properties, and defining functions for various string-related operations. Additionally, it presents exercises on global and local variables, function scopes, and string characteristics like symmetry and palindrome checking.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

A SSIGNMENT -5: S TRINGS

1. Write a function areatriangle() that takes 3 sides as commandline arguments and calculate the area
of the triangle.
2. Write a function that takes a string as a parameter and returns a string with every successive repetitive
character replaced with a star(*). For example, ‘hello’ is returned as ‘he**o’.

3. What will be the output on executing each of the statements, following the assignment statement:
address = ‘Plot-6, Nayapalli, Bhubaneswar ‘
a. len(address)
b. address[17:-1]
c. address[-len(address): len(address)]
d. address[:2] + address[-11:]
e. address.find(‘bhubaneswar’)
f. address.swapcase()
g. address.split(‘,’)
h. address.isalpha()

4. Examine the following string:


greeting = ‘hello students. How are you doing ‘
What will be the output for the following function calls:
a. greeting.count(‘you’)
b. greeting.find(‘s’)
c. greeting.rfind(‘s’)
d. greeting.capitalize()
e. greeting.lower()
f. greeting.upper()
g. greeting.swapcase()
h. greeting.istitle()
i. greeting.replace(‘hello’, ‘hai’)
j. greeting.strip()
k. greeting.split()
l. greeting.partition(‘.’)
m. greeting.startswith(‘how’)
n. greeting.endswith(‘.’)

5. Write a function that takes two strings and returns True if they are anagrams and False otherwise. A
pair of strings is anagrams if the letters in one word can be arranged to form the second one.

6. Write a function that takes a sentence as an input parameter and displays the number of words
starting with a vowel in the sentence.

7. Write a function that takes a sentence as an input parameter and replaces the first letter of every word
with the corresponding uppercase letter and rest of the letters in the word by corresponding letters in
lowercase without using built-in function.
8. Write a function that takes a string as an input and determines the count of the number of words.

9. Write a python programs to perform the following tasks:


a. Reverse a string
b. Reverse a string without reversing the words. Example:
input : ‘hello students’
output: ‘students hello’

10. Study the progr am segments given below. Give the output produced, if any .
1.
globalVar = 10
def test():
localVar = 20
print(‘Inside function test :globalVar =‘, globalVar)
print(‘Inside function test : localVar=‘, localVar)
test()
print(‘Outside function test : globalVar=‘, globalVar)
print(‘Outside function test : localVar=‘, localVar)

2.

globalVar = 10
def test():
localVar = 20
globalVar = 30
print(‘Inside function test :globalVar =‘, globalVar)
print(‘Inside function test : localVar=‘, localVar)
test()
print(‘Outside function test : globalVar=‘, globalVar)

3.

globalVar = 10
def test():
global globalVar
localVar = 20
globalVar = 30
print(‘Inside function test :globalVar =‘, globalVar)
print(‘Inside function test : localVar=‘, localVar)
test()
print(‘Outside function test : globalVar=‘, globalVar)

4.

globalVar = 10
def test():
global globalVar
localVar = 20
globalVar = 30
print(‘Inside function test :globalVar =‘, globalVar)
print(‘Inside function test : localVar=‘, localVar)
test()
print(‘Outside function test : globalVar=‘, globalVar)

5.

a=4
def f():
a=5
def g():
nonlocal a
a = 10
print(‘inside function g,’, ‘a = ‘,a)
def h():
nonlocal a
a = 20
print(‘inside function h,’, ‘a = ‘,a)
h()
g()
print(‘inside function f,’, ‘a = ‘, a)
f()

6.

x=2
def test():
x=x+1
print(x)
print(x)

7.

x=2
def test():
global x
x=x+1
print(x)
print(x)

11. Write code for the following


a. Check if a string is symmetric or asymmetric
b. Check if a string is palindrome.
c. Given a string s and index i, delete ith value from s
d. Count the number of vowels and consonants in a string.
e. Find length of a string without using inbuilt function.
f. Check if a string contains at least one digit and one alphabet.
g. Remove duplicates from a string.
h. Count frequency of characters in a string.
i. Find the character having maximum frequency in a string.

You might also like