You are on page 1of 5

Department: Information Technology

Course Name: Python


Sheet No.2
1. Given two strings, str1 and str2, str1 contains "physics", str2
contains "math", create and print out a new string named str3 that
should be contains "IT Department studies physics and math"
using the %s operator.
2. Create two string named str01 and str02, having values "Welcome
to NCT University" and "IT and Autotronics are two
Departments at NCT University", then do the following:
a) Convert str01 to the upper case and return the new value in
variable named str01_upper then print out the value of
str01_upper
b) Convert str02 to the lower case and return the new value in
variable named str02_lower then print out the value of
str02_lower
c) Concatenate str01_upper and str02_lower and return the new
value in variable named str12, calculate how many characters in
str12, then print out the contents in two different lines.
d) Rewrite str01 so that each word is printed on a separate line using
one code line.
3. Write a Python program to get a new word named new made of the
first four characters and the last two characters from a given a string
"python is a programming language"
4. Write a Python program to determine where the character (g) then
where is the character (f) are located inside the given string
"languages"

5. Create string "IT professionals may advance their careers by


obtaining IT certificates", then write a program to do the
following:
a) Count the occurrence of the "IT" word in a given string
b) Replace the "IT" word with the "ICT" word and print out the new
content
c) Check if the string is lower case, print out the result
d) Convert the string to upper case and check if it is upper case, then
printout the new values simultaneously.
e) Rewrite the string by adding "" around any IT words
f) Replace "a" character with "--XIX--"
Department: Information Technology
Course Name: Python
Solutions Sheet No.2
1. str1="physics"
str2="math"
str3="IT Department studies %s and %s" %(str1,str2)
print(str3)

2. str01="Welcome to NCT University"


str02="IT and Autotronics are two Departments at NCT University"
str01_upper=str01.upper()
print(str01_upper)
str02_lower=str02.lower()
print(str02_lower)
str12=str01_upper+" "+str02_lower
print(str12)
print(len(str12))
print("Welcome\nto\nNCT\nUniversity")

3. a="python is a programming language"


print(len(a))
new=a[0:4]+a[30:32]
print(new)

OR
4. a="python is a programming language"
print(len(a))
new=a[0:4]+a[-2:]
print(new)

5. b="languages"
print(b.index("g"))
OR print(b.find("g"))
print(b.find("f"))
OR print(b.index("f"))
The only difference is that find() method returns -1 if the substring is
not found, whereas index() throws an exception (ValueError: substring
not found).
6. str= "IT professionals may advance their careers by obtaining IT
certificates"
a=str.count("IT")
print(a)
b=str.replace("IT","ICT")
print(b)
c=str.islower()
print(c)
d=str.upper()
print(d,str.isupper())
e="\"IT\" professionals may advance their careers by obtaining \"IT\"
certificates"
print(e)
f=str.replace("a","--XIX--")
print(f)
Summry
%s : string, integer, float

%d : integer

upper()
is a built-in function, returns the uppercased string from the given
string. It converts all lowercase characters to uppercase.

lower()
is a built-in function, returns the lowercased string from the given
string. It converts all uppercase characters to lowercase.

\n to create a new line in the string

\" to display the quotation mark

\ to display the backslash

len(string)
is a built-in function, returns the length of the string.

index()
is a built-in function,
• If substring exists inside the string, it returns the lowest index in the
string where substring is found.
• If substring doesn't exist inside the string, it raises
a ValueError exception.

str.index(sub, start, end)

parameters
a) sub: substring (ex. Character, word or sentence) to be searched in
the string.
b) start and end (optional)
start: starting index within the string where search starts.
end: ending index within the string where search ends.

find()
is a built-in function, returns the lowest index of the substring if it is
found in given string. If it is not found, then it returns -1.

str.find(sub, start, end)

count()
is a built-in function, returns the number of occurrences of a substring
in the given string.
str.count(sub, start, end)

replace()
is a built-in function, returns a copy of the string where all occurrences
of a substring are replaced with another substring.

string.replace(old, new, count)


old: old substring you want to replace.
new: new substring which would replace the old substring
count: (optional) the number of times you want to replace the old
substring with the new substring.

You might also like