Swe-102 Lab 12!
Swe-102 Lab 12!
LAB # 12
STRINGS
OBJECTIVE
Working on the strings formatting.
THEORY
Strings:
A string is a sequence of characters which are surrounded by either single quotation
marks, or double quotation marks. A str object is immutable; that is, its content cannot
be changed once the string is created.
Creating Strings:
Create strings by using the constructor, as follows:
s1 = str() # Create an empty string object
s2 = str("Welcome") # Create a string object for Welcome
Example:
s = input("Enter a string: ")
if len(s) % 2 == 0:
print(s, "contains an even number of characters")
else:
print(s, "contains an odd number of characters")
Output:
>>> %Run task1.py
Enter a string: Strings
Strings contains an odd number of characters
>>>
Example:
s = "Welcome"
print(s[-2])
print(s[0 : 4])
print(s[4 : ])
Output:
>>> %Run task2.py
m
Welc
ome
Comparing Strings:
Compare the strings by using the comparison operators (==,!=, >, >=, <, and <=).
Python compares strings by comparing their corresponding characters, and it does this
by evaluating the characters’ numeric codes. The ASCII Character Set, to find the
numeric codes for characters.
Example:
s1 = input("Enter the first string: ")
s2 = input("Enter the second string: ")
if s2 < s1:
s1, s2 = s2, s1
print("The two strings are in this order:", s1, s2)
Output:
>>> %Run task3.py
Enter the first string: aza
Enter the second string: abz
The two strings are in this order: abz aza
Converting Strings
The str class contains these methods for converting letter cases in strings and for
replacing one string with another.
capitalize(): str Returns a copy of this string with only the first character
capitalized
lower(): str Returns a copy of this string with all letters converted to
lowercase.
upper(): str Returns a copy of this string with all letters converted to
uppercase.
title(): str Returns a copy of this string with the first letter capitalized in
each word.
swapcase(): str Returns a copy of this string in which lowercase letters are
converted to uppercase and uppercase to lowercase.
replace(old, new): str Returns a new string that replaces all the occurrences of the
old string with a new string.
Programming Fundamentals (SWE-102) SSUET/QR/114
EXERCISE
A. Point out the errors, if any, and paste the output also in the following Python
programs.
1. Code:
a = "PYTHON"
a[0] = "x"
#Apply Exception for mention error
Output:
2. Code:
a = STRING
i = 0
while i < len(b):
c = a[i]
print(c)
i+=i + 1
Output:
3. Code:
Def 1my_function(x):
return x[::-1]
Output:
Programming Fundamentals (SWE-102) SSUET/QR/114
1. Code:
s= "Welcome"
for i in range(0, len(s), 2):
print(s[i], end = '')
Output:
2. Code:
s = input("Enter a string: ")
if "Python" in s:
print("Python", "is in", s)
else:
print("Python", "is not in", s)
Output:
3. Code:
str='cold'
list_enumerate=list(enumerate(str))
print("list enumerate:", list_enumerate)
print("list length:", len(str))
s1 = "Welcome to Python"
s2 = s1.replace("o","abc")
print(s2)
a = "Python" + "String"
b = "<" + a*3 + ">"
print(b)
Output:
Programming Fundamentals (SWE-102) SSUET/QR/114
1. Write a program that Store a person’s name, and include some whitespace
characters at the beginning and end of the name. Make sure you use each character
combination, "\t" and "\n", at least once. Print the name once, so the whitespace
around the name is displayed. Then print the name using each of the three stripping
functions, lstrip(),rstrip(), and strip().
2. Write a program that asks the user for their favourite color. Create the following
output (assuming blue is the chosen color) (hint: use ‘+’ and ‘*’)
blueblueblueblueblueblueblueblueblueblue
blue blue
blueblueblueblueblueblueblueblueblueblue