How to write a program to accepts a string from console and print the characters that have even indexes?

Example:

If the following string is given as input to the program:
H1e2l3l4o5
Then, the output of the program should be:
Hello

print("Please enter text to print::")
inputchars = input()

if inputchars:
	string = ""
	for i in inputchars:
		if inputchars.index(i)%2 == 0:
			string += str(i)
	
	print('-------------------')
	print("You Entered:", inputchars)
	print("Result:")
	print(string)
# OUTPUT::
Please enter text to print::
H1e2l3l4o5
-------------------
You Entered: H1e2l3l4o5
Result:
Hello

How to find the odd and even numbers using Python?

Here, In below code, user can enter two numbers to find the odd and even numbers between them. The number must be in between 1000 and 3000.

print("Please Enter the Number between 1000 and 3000")

print("Enter the First Number:")
first_number = input()

print("Enter the Second Number:")
second_number = input()

print("First Number: ", first_number)
print("Second Number: ", second_number)


if int(first_number) >= 1000 and int(second_number) <= 3000:
	first_number = int(first_number)
	second_number = int(second_number)

	even_numbers= []
	odd_numbers = []

	for i in range(first_number,(second_number+1)):
		if i%2 is 0:
			even_numbers.append(str(i))
		else: 
			odd_numbers.append(str(i))
	
else:
	print("Please enter a number between 1000 and 3000")

How to read or write files in Python?

Before any files to read we have to know how to Open & Close the files.

# Open Function:

we can open a file using python build in function open()

file_object = open(file_name, file_access_mode )
  • file_name = It is the file name or the full file path.
  • file_access_mode = File access mode is to determine, open file for read, write , append etc.
ModesDescription
rOpens a file for read only purpose.
rbOpen a file for reading only in binary format.
r+Open a file for both operation reading & writing.
rb+Open a file for both reading & writing in binary format.
wOpen a file for writing purpose only.
It overwrites the existing file or otherwise create a new file.
wbOpens a file for writing only in binary format.
It overwrites the existing file or otherwise create a new file.
aOpens a file for appending.
abOpens a file for appending in binary format.
ab+Opens a file for both appending & reading in binary format.
w+Opens a file for both reading & writing format.
wb+Opens a file for both reading & writing in binary format.

# Write Function:


file_content = "This is my First Text."
file_object.write( file_content ) 

# Read Function:

file_object.read()

# Rename Files:

os.rename( old_file_name, new_file_name )

# Delete File:

os.remove( file_name )

# Close File:


file_object.close()

# File Object Attributes:

  • file_object.closed
  • file_object.mode
  • file_object.softspace [ It is no longer supported in python 3.x ]
  • file_object.name

How to redirect to https with www using .htaccess?

First check in your server

Redirect to https and www:

By using the .htaccess technique redirects to the https and www versions of your web pages to https://www.example.com. Add to site’s root .htaccess file:


RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$  [NC]
RewriteRule (.*) https://www.%1/$1 [R=301,L]

Here, Checks if HTTPS is off, or if the request does not include www,
If either condition matches, the request follow the rule and it is redirected to the https://www.example.com

Redirect to https and non-www


RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) https://example.com/$1 [L,R=301]