0% found this document useful (0 votes)
2 views12 pages

Tutorial 6

This document serves as a tutorial on string manipulation and file handling in programming. It covers string slicing, methods for checking character types, converting strings, and file operations including reading, writing, and appending data. Additionally, it includes practical exercises for checking substrings, validating passwords, finding common prefixes, and handling file content.

Uploaded by

1620040208
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)
2 views12 pages

Tutorial 6

This document serves as a tutorial on string manipulation and file handling in programming. It covers string slicing, methods for checking character types, converting strings, and file operations including reading, writing, and appending data. Additionally, it includes practical exercises for checking substrings, validating passwords, finding common prefixes, and handling file content.

Uploaded by

1620040208
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/ 12

INTRODUCTION TO COMPUTER SCIENCE:

PROGRAMMING METHODOLOGY
TUTORIAL 6
STRINGS AND FILES
String-Index operator

Ø2. Slicing operator [start:end](start->end-1): s[start:end] cuts the


string s beginning from elements with index start to end-1.
Manipulating strings
Ø3. A way to delete an element in a string s: s=s[:i]+s[i+1:] can
delete the element with index i.
Ø4. A way to change an element in a string s: example:
s=‘Walcome’, then s=s[:1]+’e’+s[2:] will change s to
‘Welcome’. Notice: Strings’ elements cannot be directly changed.
So here it is wrong to write s[1]=‘e’.
Ø5. len(s) returns the length of string s.
Ø6. “in” operator: character ch is included in string s, ch in s-
>True, otherwise ch in s->False.
Ø7. Strings can also act as a collection of elements in for loop:
for i in s:.
Some methods of strings-I
Ø 8. Methods in str class to judge types of characters in strings.
Ø i)s.isalnum(), ii)s.isalpha(), iii)s.isdigit(), iv)s.islower(), v)s.isupper()
judge whether s contains i)only letters and numbers, ii) only letters, iii)only
numbers, iv)all letters in s are lowercase, v)all letters in s are uppercase.
True or False will be returned.
Ø 9. Methods in str class to manipulate substrings.
Ø i)s.endswith(s1) returns True if the string s ends with substring s1.
Ø ii)s.startswith(s1) returns True if the string s starts with substring s1.
Ø iii)s.find(s1) returns the lowest index where s1 starts in the string s, or -1
if s1 is not found; similarly, s.rfind(s1) returns the highest index.
Ø iv)s.count(s1) returns the number of non-overlapping occurrences of the
substring s1.
Some methods of strings-II
Ø 10. Methods in str class to convert strings.
Ø i)s.lower(): returns a copy of the string s with all letters converted to
lowercase. Similarly, s.upper() uppercase.
Ø ii)s.swapcase(): returns a copy of the string s in which lowercase letters are
converted to uppercase and uppercase to lowercase.
Ø iii)s.replace(old,new): returns a new string that replaces all the occurrences
of the old substring in s with a new substring.
Ø 11. Splitting a string into a list: examples:
Ø i) items=‘Jane John Peter Susan’.split() -
>[‘Jane’,’John’,’Peter’,‘Susan’]
Ø ii) items=’03/07/2019’.split(‘/’)->[‘03’,’07’,’2019’].
Text input and output-I
fileVariable = open(filename, mode)
Ø Three modes:
“r” – Opens a file for reading
“w” – Opens a file for writing
“a” – Opens a file for appending data from the end of the file
Text input and output-II
Ø Write function will write strings to a file. If it does
not exist, a new file will be created.
Text input and output-III

Ø Return all the content.

Ø Return 3 characters.

Ø Return a line.

Ø Return every line as a list.


Q1: Check substrings
ØYou can check whether a string is a substring of another string by
using the find method in the str class. Write your own function to
implement find. Write a program that prompts the user to enter two
strings and then checks whether the first string is a substring of
the second string.

Write your own


program to
realize the
function of
find method.
Q2: Check password

ØSome Web sites impose certain rules for passwords. Write


a function that checks whether a string is a valid
password. Suppose the password rules are as follows:

Ø A password must have at least eight characters. len()


Ø A password must consist of only letters and digits. isalnum()
Ø A password must contain at least two digits. isdigit()
Q3: Longest common prefix
ØWrite a method that returns the longest common prefix of
two strings. For example, the longest common prefix of
distance and disinfection is dis. The header of the
method is def prefix(s1,s2): . If the two strings have
no common prefix, the method returns an
empty string. Write a main method that prompts the user
to enter two strings and display their longest common
prefix.
Q4: Handling files
Øi) Write a program that will count the number of characters, words,
and lines in a file. Words are separated by a whitespace character.
Your program should prompt the user to enter a filename.

Øii) In the file “JaneEyre.txt”, there are some typos “amd”


which should actually be “and”, find how many of them and
replace all, then write the correct version into a new file.

You might also like