Python string – replace()

replace() is an inbuilt function in Python programming language that returns a copy of the string where all occurrences of a substring is replaced with another substring.

Syntax: string.replace(old, new, count)

old – old substring you want to replace.

new – new substring which would replace the old substring.

count – the number of times you want to replace the old substring with the new substring. (Optional)

[mai mult...]

How to calculate the number of days between two dates in javascript?

Calculating the number of days between two dates in JavaScript required to use date object for any kind of calculation. For that, first, get the internal millisecond value of the date using the in-built JavaScript getTime() function. As soon as both the dates get converted, proceed further by subtracting the later one from the earlier one which in turn returns the difference in milliseconds. Later, the final result can be calculated by dividing the difference (which is in milliseconds) of both the dates by the number of milliseconds in one day.

Define two dates using new Date().
Calculate the time difference of two dates using date2.getTime() – date1.getTime();
Calculate the no. of days between two dates, divide the time difference of both the dates by no. of milliseconds in a day (1000*60*60*24)
Print the final result using document.write().

[mai mult...]

Read a file line by line in Python

Reading line by line using readlines().
readlines() is used to read all the lines at a single go and then return them as each line a string element in a list. This function can be used for small files, as it reads the whole file content to the memory, then split it into separate lines. We can iterate over the list and strip the newline ‘\n’ character using strip() function.

[mai mult...]

Writing to an text file using Python

There are two ways to write in a file:

  1. write() : Inserts the string str1 in a single line in the text file.File_object.write(str1)
  2. writelines() : For a list of string elements, each string is inserted in the text file. Used to insert multiple strings at a single time.File_object.writelines(L) for L = [str1, str2, str3]
[mai mult...]