BPLCK205B Module III Important Questions
BPLCK205B Module III Important Questions
BPLCK205B -105B
MODULE - III
Disclaimer:
•
• 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:
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.