Introduction to python programming
BPLCK205B -105B
Very Important Questions With Answers
MODULE - III
Disclaimer:
● At LearnyHive, we serve as a reference hub where resources from
various trusted
sources cater to your academic needs.
● We regularly update and organize these study materials to ensure they
remain current and accessible for your convenience.
1.Explain python string handling methods with examples: split(), endswith(), ljust(),
center(), lstrip(), join(), startswith(), rjust(), strip(), rstrip().
• startswith() and endswith()
• Explain python string handling methods with examples: split(), endswith(), ljust(), center(), lstrip(),
join(), startswith(), rjust(), strip(), rstrip().
• These methods are useful alternatives to the == equals operator if we need to check only whether the first
or last part of the string, rather than the whole thing, is equal to another string.
• EX:
•
• Join() and split()
• The join() method is useful when we have a list of strings that need to be joined together into a single
string value.
• The join() method is called on a string, gets passed a list of strings, and returns a string. The returned
string is the concatenation of each string in the passed-in list
• The split() method is called on a string value and returns a list of strings.
• We can pass a delimiter string to the split() method to specify a different string to split upon.
• A common use of split() is to split a multiline string along the newline characters.
• Passing split() the argument '\n' lets us split the multiline string stored in spam along the newlines and
return a list in which each item corresponds to one line of the string.
• EX:
•
• rjust(), ljust(),center()
• The rjust() and ljust() string methods return a padded version of the string they are called on, with spaces
inserted to justify the text.
• The first argument to both methods is an integer length for the justified string.
• An optional second argument to rjust() and ljust() will specify a fill character other than a space
character.
• The center() string method works like ljust() and rjust() but centers the text rather than justifying it to the
left or right.
• These methods are especially useful when you need to print tabular data that has the correct spacing.
• EX:
•
• strip(), rstrip(), and lstrip()
• The strip() string method will return a new string without any whitespace characters at the beginning or
end.
• The lstrip() and rstrip() methods will remove whitespace characters from the left and right ends,
respectively.
• Optionally, a string argument will specify which characters on the ends should be stripped.
• Passing strip() the argument 'ampS' will tell it to strip occurences of a, m, p, and capital S from the ends
of the string stored in spam.
• The order of the characters in the string passed to strip() does not matter: strip('ampS') will do the same
thing as strip('mapS') or strip('Spam').
• EX:
2.Explain with suitable python program segments: a) os.path.basename() b)
os.path.join() c) os.path.dirname() d) os.path.split().
•
3.Develop a python program to find the total size of all the files in the given directory.
4.Develop a python program to read and print the contents of a text file.
5.Explain the three steps for reading or writing files in python with examples.
6.Explain how to save variables using pprint.pformat() functions with suitable code
snippet.
• The pprint.pprint() function will “pretty print” the contents of a list or dictionary to the screen, while the
pprint.pformat() function will return this same text as a string instead of printing it.
• Not only is this string formatted to be easy to read, but it is also syntactically correct Python code Using
pprint.pformat() will give you a string that you can write to .py file.
• This file will be your very own module that you can import whenever you want to use the variable stored
in it.
• EX:
•
7.Explain the different functions for finding the size of a file and its folder contents with
examples.
8.Explain indexing and slicing technique using strings with examples.
• Strings use indexes and slices the same way lists do. We can think of the string 'Hello world!' as a list and
each character in the string as an item with a corresponding index.
• The space and exclamation point are included in the character count, so 'Hello world!' is 12 characters
long
• If we specify an index, you’ll get the character at that position in the string
• The substring we get from spam[0:5] will include everything from spam[0] to spam[4], leaving out the
space at index 5.
• EX:
•
9.Explain string methods the upper(), lower(), isupper(), and islower() .
• The upper() and lower() string methods return a new string where all the letters in the original string have
been converted to uppercase or lowercase, respectively.
• These methods do not change the string itself but return new string values.
• The upper() and lower() methods are helpful if we need to make a case-insensitive comparison
• The isupper() and islower() methods will return a Boolean True value if the string has at least one letter
and all the letters are uppercase or lowercase, respectively. Otherwise, the method returns False