What are the different file processing modes supported by Python?

Python provides three modes to open files. The read-only, write-only, read-write and append mode. ‘r’ is used to open a file in read-only mode, ‘w’ is used to open a file in write-only mode, ‘rw’ is used to open in reading and write mode, ‘a’ is used to open a file in append mode. If … Read more

Which are the file related libraries/modules in Python?

The Python provides libraries/modules that enable you to manipulate text files and binary files on the file system. It helps to create files, update their contents, copy, and delete files. The libraries are os, os.path, and shutil. Here, os and os.path – modules include a function for accessing the filesystem while shutil – module enables … Read more

Give an example of shuffle() method?

This method shuffles the given string or an array. It randomizes the items in the array. This method is present in the random module. So, we need to import it and then we can call the function. It shuffles elements each time when the function calls and produces different output. import random list = [12,25,15,65,58,14,5,]; … Read more

Why do we use join() function in Python?

The join() is defined as a string method which returns a string value. It is concatenated with the elements of an iterable. It provides a flexible way to concatenate the strings. See an example below. str = “Rohan” str2 = “ab” # Calling function str2 = str.join(str2) # Displaying result print(str2) Output: aRohanb In Python, … Read more

How to remove whitespaces from a string in Python?

To remove the whitespaces and trailing spaces from the string, Python providies strip([str]) built-in function. This function returns a copy of the string after removing whitespaces if present. Otherwise returns original string. string = ” javatpoint “ string2 = ” javatpoint “ string3 = ” javatpoint” print(string) print(string2) print(string3) print(“After stripping all have placed in … Read more