0% found this document useful (0 votes)
39 views

Swe-102 Lab 12!

This document discusses strings in Python including: 1) Strings are sequences of characters that can be accessed using indexes. Strings are immutable so their content cannot be changed. 2) Common string methods include capitalize(), lower(), upper(), title(), swapcase(), replace(), lstrip(), rstrip(), and strip(). 3) Strings can be compared using comparison operators like == and <. They are compared character by character based on ASCII values. 4) Examples are provided to demonstrate accessing characters, comparing strings, converting cases, stripping whitespace, and replacing substrings.

Uploaded by

Shahzaib Shakir
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Swe-102 Lab 12!

This document discusses strings in Python including: 1) Strings are sequences of characters that can be accessed using indexes. Strings are immutable so their content cannot be changed. 2) Common string methods include capitalize(), lower(), upper(), title(), swapcase(), replace(), lstrip(), rstrip(), and strip(). 3) Strings can be compared using comparison operators like == and <. They are compared character by character based on ASCII values. 4) Examples are provided to demonstrate accessing characters, comparing strings, converting cases, stripping whitespace, and replacing substrings.

Uploaded by

Shahzaib Shakir
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Programming Fundamentals (SWE-102) SSUET/QR/114

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

Create strings by simple syntax of a string value


s1 = " " # Same as s1 = str()
s2 = "Welcome" # Same as s2 = str("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
>>>

Access characters in a string:


A character in the string can be accessed through the index operator using the syntax:
s[index], the indexes are 0 based, that range from 0 to len(s)-1.
Programming Fundamentals (SWE-102) SSUET/QR/114

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

Stripping Whitespace Characters from a String:


Use the following methods to strip whitespace characters from the front, end, or both
the front and end of a string. Recall that the characters ' ', \t, \f, \r, and \n are called the
whitespace characters. Following methods are : lstrip(), rstrip(), strip().

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]

mytxt = 1my_function("I wonder how this text looks like


backwards")
print(mytxt)

Output:
Programming Fundamentals (SWE-102) SSUET/QR/114

B. What would be the output of the following programs:

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

C. Write Python programs for the following:

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

You might also like